2

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

ernest_k
  • 44,416
  • 5
  • 53
  • 99
Lenny
  • 83
  • 1
  • 1
  • 11

2 Answers2

2

When child class Instance becomes Parent class Instance

It doesn't "become". It "is". A Child is a Parent, just as a Banana is a Fruit.

And this represents the current class Instance

this represents the current class instance. There's only one object at runtime, not multiple objects, not multiple layers based on the class hierarchy. There's one object. Period.

And this represents the current class Instance, So why am getting at 4 Child's class name ?

Every instance/object that lives at runtime was created with the new keyword. The class with which new was used, is the runtime class of that object (that's a simplified statement). The runtime class is the one that is returned when you call getClass().

In all of these cases, Child is the runtime class of the instance that obj points to:

Child obj = new Child();
Parent obj = new Child();
//and all the possible intermediate parents, until
Object obj = new Child();

OR When extended, Does it creates a two copies of Methods for respective parent and child classes which are inherited in memory ?

The easiest way to think of this is:

  • When an object is created, Java knows the runtime class of that object (see above).
  • With the runtime class, it knows what methods and attributes are available to that object that are declared in that runtime class (Child, in your case)
  • With the runtime class, it knows what methods and attributes are inherited from that object's parent types (Parent, and Object, in your case)
  • With the runtime class, Java knows what methods inherited by and overridden by it will be executed as per the child class (you don't have overridden methods).

So when you write this, you refer to the single object with which the current code is called. And what you can reference using this is methods and attributes declared in the runtime class as well and those accessible from the super-types.

When you write super, you still refer to the single object with which the current code is called. And what you reference using super is explicitly methods or attributes declared in the parent class of the one declaring the currently executing code.

ernest_k
  • 44,416
  • 5
  • 53
  • 99
  • Okay..makes sense..But lets say am in my parent's house or class like my code mentioned in Question, then Is there any way I could set my child's class property too from my parent's class....? – Lenny Mar 04 '19 at 06:44
  • 1
    @Lenny `Parent` doesn't know about `Child` (and it shouldn't). The only proper way to do that is to expose a method in a class that is overridden in the child (like overriding `setLog4jConfig` in `Child` and making it set a property of `Child`) – ernest_k Mar 04 '19 at 06:50
0

You're seeing the output of "Child" because that's what obj is; this is the current object at runtime, not in terms of where methods are in your source code. That is one of the nice properties of object oriented programming; a parent class can define a method that relies on methods that are implemented (or overridden) in subclasses. If, for example, setLog4jConfig were overridden in the child class, then there would be two copies of the method. See this explanation of vtables.

Josh
  • 500
  • 3
  • 6
  • Thanks for sharing vtables link..It makes much more sense. One more question Here...Does compiler refers any tables too where it could check which super class actually can points to child's class instance or Interface can points to implemented class's instance ? – Lenny Mar 09 '19 at 17:07
  • I'm not sure what you mean by *point to*, but the compiler will certainly understand that a reference variable declared with a superclass type can point to an instance of a subclass, e.g. `Animal a = new Parrot();`. At runtime you can also use `isAssignableFrom` to determine such a relationship. See https://stackoverflow.com/questions/3949260/java-class-isinstance-vs-class-isassignablefrom – Josh Mar 09 '19 at 23:04