Ishwar Chandra
Ishwar Chandra

Reputation: 75

why can we not return super keyword in java unlike this keyword

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

Answers (2)

Raildex
Raildex

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

Giorgi Tsiklauri
Giorgi Tsiklauri

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:

  1. When we instantiate an object of let's say type 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 our class X extends Y and class Y extends Z, people think, that executing new X(); will implicitly execute new Y() (as constructor of X calls constructor of Y), then new Z();(as constructor of Y calls constructor of Z), and finally new Object(), as Object is the top-most super class of entire ecosystem in Java. This assumption is wrong, as during the instantiation of the object new X(); only instance which gets created in the heap is the instance of class X. Hence, returning super from the object's method will not work, as there's no super-class instance, on the heap, to be returned;

  1. If the parent classes are not instantiated, then how the members of the super classes are made available to the child class? are they copied to the derived child classes?

Not exactly copied, however, bytecode of the parent class members (fields, methods) [are referenced at run-time] from the child class1.

Upvotes: 1

Related Questions