The instance method display() of the superclass, SUP is accessing its private String variable s. The subclass, SUB does not inherit the private variable s, but new SUB().display(); compiles and runs fine.
I am confused about this. Please help.
No, that is perfectly ok. If it wasn't allowed for a class to access its own private members from public or protected methods then private members would be completely useless.
The subclass is not directly accessing the private variable. In fact it isn't even aware it exists. It simply calls the method and lets it do it's thing.
What a stench! Central nervous system shutting down. Save yourself tiny ad!