The output of this simple program is This is base
.
public class mainApp{
private void func(){
System.out.println("This is base");
}
public static void main(String[] args){
mainApp newObj = new derived();
newObj.func();
}
}
class derived extends mainApp{
public void func(){
System.out.println("This is derived");
}
}
My question is when we are using this line
mainApp newObj = new derived();
are we not actually creating an object of derived class using a reference of base class mainApp. So, when I am using the object to call it's method why don't I get the method from the derived class? Why I get the method from the base class.using this line,
mainApp newObj = new derived();
, are we working with a reference of mainApp OR we are working with an object of derived class. Which one is correct?