I'm trying to understand how child class's instance becomes parent's class instance. I mean what kind of model they get in memory once created.
Check below code snippet
public class Parent {
public String log4jConfig;
public void setLog4jConfig(String log4jConfig){
System.out.println(this.getClass()); ---4
this.log4jConfig = log4jConfig;
}
public String getLog4jConfig() {
return log4jConfig;
}
}
public class Child extends Parent {
}
public class Mainclass {
public static void main(String[] args) {
Child obj = new Child(); // ---1
obj.setLog4jConfig("log4jConfig");
}
}
When I create an instance of child class, It can access all methods of parent's class because those are inherited.
And this
represents the current class Instance, So why am getting at 4 Child's class name ? So if CHILD IS PARENT when extended then how it sits in memory, I'm trying to visualize the sitting plan of Parent and child class in memory where this
of parent becomes Child's instance.
OR When extended, Does it creates a two copies of Methods for respective parent and child classes which are inherited in memory ?
Any Help Please..!
Thanks