1

Super keyword related doubt.

class Parent{
    int x=40;
    void show()
    {
        System.out.println("Parent");
    }
}

class Child extends Parent
{   int x=20;
    void show()      //method overriding has been done
    {

        System.out.println(super.x); // prints parent data member
        System.out.println(((Parent)this).x); /*same output as previous statement which means super is similar to (Parent)this*/ 
        System.out.println("child");
        super.show();  // invokes parent show() method  
        ((Parent)this).show(); //Doesnt invoke parent show() method.Why?
    }

public static void main(String s[])
{ 
    Child c1=new Child(); //Child class object
    c1.show();  
}}

So, System.out.println(super.x) and System.out.println(((Parent)this).x) prints the same value.So if super.show() calls parent class show() method then why is ((Parent)this).show(); unable to call parent show()? Please tell appropriate explaination for this.

  • 3
    You should probably fix the indentation of your code, then someone will answer your question. – Murat Karagöz Mar 14 '17 at 13:26
  • 2
    what's the point of casting the `this` pointer to the super class if you can just call the method (because it is defined in the parent), so you can just use `this.show();`, `super.show()` or just `show()` !? unless you have overridden the `show`method in your child class then super.show() calls the parent instead of the local one. – xander Mar 14 '17 at 13:31
  • 1
    `((Base)this).show(); //Doesnt invokes parent show() method.Why?`.. what is `Base`? it should be `((Parent)this).show()` – Dazak Mar 14 '17 at 13:34
  • You call to `((Parent) this).show(); ` (I think this was what you intended) inside `Child.show()` still hits `Child.show()`, so it’s a recursive call (leading to infinite recursion). I think this answers your question. – Ole V.V. Mar 14 '17 at 13:35
  • 1
    "Doesnt invoke parent show() method.Why?" For the same reason that invoking this: `Integer integer = Integer.valueOf(0); System.out.println(((Object) integer).toString());` doesn't invoke `Object.toString()`. – Andy Turner Mar 14 '17 at 13:37
  • Sorry, right I intended to write ((Parent)this).show(); – Karan Sachy Mar 14 '17 at 13:38
  • Ole V.V. , I do agree, its leading to infinite recursion but super is similar to ((Parent)this) in particular code – Karan Sachy Mar 14 '17 at 13:42

2 Answers2

1

Constructor Chaining in Java keyword this represent current object and when one class extends another then its super class reference variable may hold child class reference variable that's is in your code is ((Parent)this).x ,

while super keyword is used to call directly super class constructor and its variables.

at the same time when super class variables holds child class object and when we use super it refers same object .

How to call one constructor from another constructor in Java or What is Constructor Chaining in Java is one of the tricky questions in Java interviews. Well, you can use this keyword to call one constructor from another constructor of the same class if you want to call a constructor from based class or super class then you can use super keyword. Calling one constructor from other is called Constructor chaining in Java. Constructors can call each other automatically or explicitly using this() and super() keywords. this() denotes a no-argument constructor of the same class and super() denotes a no argument or default constructor of parent class. Also having multiple constructors in the same class is known as constructor overloading in Java.

Read more: http://www.java67.com/2012/12/how-constructor-chaining-works-in-java.html#ixzz4bJ5C069o

Chetan Joshi
  • 5,582
  • 4
  • 30
  • 43
0

Calling ((Parent) this).show(); on what is really a Child object causes Child.show() to be called. Whether you are doing it as myObject.show() or through this makes no difference for which version of the method gets called — it’s always the method determined by the object’s runtime type, in this case Child.

So you have a recursive call, leading to infinite recursion.

super.show() on the other hand calls the method in the super class, in this case Parent.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161