In the below code snippet, the result is really confusing.
public class TestInheritance {
public static void main(String[] args) {
new Son();
While methods can be overridden, attributes can be hidden.
In your case, the attribute x
is hidden: in your Son
class, you can't access the Father
's x
value unless you use the super
keyword. The Father
class doesn't know about the Son
's x
attribute.
In the opposit, the toString()
method is overriden: the implementation that will always be called is the one of the instantiated class (unless it does not override it), i.e. in your case Son
, whatever the variable's type (Object
, Father
...).