if ii run this code I get output 5. That's ok because method hiding is going on here. But when I delete the void print() of Sup class, I get an error at sb.print(). Someone please tell me why is this error coming even though code has to run print() of Sub class. Then why compiler is checking for print() of Sup class.
because you use reference of type super class. what method you can invoke determined by type of reference. actual method is method of actual object because overriding
If you delete m() from Super how you will call supRef.m(); at line 9? (this for example one of reason why you get compiler error)
I get output 5. That's ok because method hiding is going on here
and you get 5 because not hiding but because overriding, not hiding.
Hiding is when you use static method in super class and child with same name same signature.
I gone through many question related to this question and understood that the variable hiding depends on the class type and method overriding depends upon object type. But just look at my another problem
now,if variable hiding depends on class type then why the output of this code is 5.
And if I write int x=5; in Sub(), then output is 2. help me out !!!