1

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;
    }
}
  • 2
    "Although super is an instance of child class": no it isn't. – Henry Jul 18 '20 at 17:21
  • Why do you want to do this? There is no other "super" instance you can use, it's the same object and its type is `B`. – Progman Jul 18 '20 at 17:21
  • That is not how you can use `super`, you should use `return this`. `super` is only allowed to call a superclass constructor, or to be able to call the implementation of a method in a superclass (instead of the overridden version in the current class). – Mark Rotteveel Jul 18 '20 at 17:24
  • @Henry "Although super is an instance of child class": Yes, it is. This can be 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 is: true true – Ishwar Chandra Jul 18 '20 at 17:48
  • @MarkRotteveel , As mentioned in my previous comment, If super is the instance of child class and java allows us to return any instance as per return type then why can not we return "super" as an instance just like this instance. – Ishwar Chandra Jul 18 '20 at 17:58
  • 4
    @IshwarChandra have a look at the specification: https://docs.oracle.com/javase/specs/jls/se14/html/jls-15.html#jls-15.8, while `this` is a primary expression, `super` is not. – Henry Jul 18 '20 at 18:02
  • 3
    Because there is no need for that, that is what `this` is for. Allowing you to return `super` would be ambiguous given how `super.someMethod()` works. If you want the nitty-gritty, read the JLS. – Mark Rotteveel Jul 18 '20 at 18:03
  • @IshwarChandra No. This snippet doesn't demonstrate anything, as it only shows that a B instance is an instance of B, [which is self-evident](https://stackoverflow.com/questions/9790210). In your own terms, this would demonstrate that "a child class is an instance of super", which is true by definition (see [this answer](https://stackoverflow.com/a/3294759) for why `this.getClass()` is equivalent to `super.getClass()`; in short, `getClass` is `final`, so you're actually calling `Object`'s implementation, which just returns the class of `this`.) – Salem Jul 18 '20 at 18:38

2 Answers2

1

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
      }
}
codebod
  • 686
  • 6
  • 17
Raildex
  • 3,406
  • 1
  • 18
  • 42
1

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.

Giorgi Tsiklauri
  • 9,715
  • 8
  • 45
  • 66