Reputation: 75
super
keyword can be used to access the members of parent class. Apart from this, super
is also an instance of child class as justified by below code:
class A {
}
class B extends A {
public void method() {
System.out.println(this.getClass().isInstance(new B()));
System.out.println(super.getClass().isInstance(new B()));
}
}
public class InheritanceDemo {
public static void main(String[] args) {
B obj = new B();
obj.method();
}
}
Output: true true
Now, If super
is the instance of child class as justified above and java allows us to return any instance as per return type then why can we not return super
as an instance(I tried it with the help of below code but got compile error). On the other hand, we can return this
as an instance.
class A {
}
class B extends A {
public B methodThis() {
return this;
}
public A methodSuper() {
return super;
}
}
Upvotes: 1
Views: 645
Reputation: 4765
this
represents the current instance of an object.
What do you think super
would represent?
super
is a reserved keyword in java and is used to call an implementation (of a method or constructor) of a base class.
public class A {
public int testVar = 300;
}
public class B extends A {
public int testVar = 400;
public int getSuper() {
return this.testVar; // returns 400
}
public int getThis() {
return super.testVar; // returns 300
}
}
Upvotes: 1
Reputation: 11138
TL;DR - new SomeType()
creates and allocates only one object, an instance of SomeType
in the heap, and it doesn't create objects of entire chain of SomeType
's parent classes. Members of the parent classes, however, are referenced dynamically at run-time. So, while you can access the super-class member with super
keyword, it doesn't mean that instances of the super-classes are also created.
Details:
There are two important concepts to understand in order to fully answer your question:
X
, are the objects of its immediate parent and grandparent and etc. (entire hierarchy of the parent classes, starting from the type in question, until and including Object class) also created?Due to the fact, that every constructor does an implicit call, as its first instruction, to its immediate parent class constructor with
super()
, some people think, that JVM's runtime system instantiates entire chain of the parent classes as well. If ourclass X extends Y
andclass Y extends Z
, people think, that executingnew X();
will implicitly executenew Y()
(as constructor ofX
calls constructor ofY
), thennew Z();
(as constructor ofY
calls constructor ofZ
), and finallynew Object()
, asObject
is the top-most super class of entire ecosystem inJava
. This assumption is wrong, as during the instantiation of the objectnew X();
only instance which gets created in the heap is the instance of classX
. Hence, returningsuper
from the object's method will not work, as there's no super-class instance, on the heap, to be returned;
Not exactly copied, however, bytecode of the parent class members (fields, methods) [are referenced at run-time] from the child class1.
Upvotes: 1