In the below code snippet, the result is really confusing.
public class TestInheritance {
public static void main(String[] args) {
new Son();
This is a behaviour done specially to have access to private members. So this.x looks at the variable X which is declared for Father, but when you pass this as a parameter to System.out.println
in a method in Father - it looks at the method to call depending on the type of the parameter - in your case Son.
So how do you call the super classes method? Using super.toString()
, etc.
From Father it cannot access the x variable of Son.