Look at the code below:
class Rodent {
private String hello = "hello" ;
public void printHello() {
System.out.println(hello);
}
}
public class Mouse extends Rodent {
public static void main(String[] args) {
Mouse mouse = new Mouse();
mouse.printHello();
}
}
If we see , the method printHello() is inherited. This method is inside using a private variable. But this private variable is not inherited. Then how does this code works ?
Is it something similar like closure in JavaScript ?
If method printHello()
is inherited and it needs some private variable , then this also has to be made available in Mouse
as now I am calling printHello()
in Mouse
?
Edit : For those who say that parent class method will be called , I have found that there is only object created and not separate objects one for sub class and one for parent class. Here