4

I am trying to find proof for the statement - keyword super is the reference to parent class just like keyword this is the reference to current class.

I am trying multilevel Inheritance in Java A->B->C: class A is grand parent, class B is parent, class C is child.

I have a variable X declared in all three classes with values respectively (A:x=100,B:x=200,C:x=300)

In the child class constructor I am printing values. However the casting isn't working for super keyword whereas it's working for this keyword.

((A)super).x is not working, but ((A)this).x is working.

class A {
    int x = 100;
}

class B extends A {
    int x = 200;
}

public class C extends B {
    int x = 300;
    public C () {
        System.out.println(this.x); //OP = 300
        System.out.println(super.x); // OP = 200
        System.out.println(((A)this).x);// OP = 100
        System.out.println(((A)super).x); // Giving Compile time Error.. Why?
        B reftoB = new B();
        System.out.println(((A)reftoB).x);  // OP = 100
    }

    public static void main(String[] args) {
        C t1= new C();
    }
}

I expect the output of System.out.println(((A)super).x) is 100, but it is giving a compile time error.

So my question is if super is a reference to the parent class then why isn't type casting working on it?

Greg Schmidt
  • 5,010
  • 2
  • 14
  • 35
  • 8
    It's just not valid syntax. `super` can't be used that way. It can only be used in a direct field access/method invocation expression, with none of that attempt to cast. – Sotirios Delimanolis Jan 30 '19 at 01:27
  • 2
    `super` refers to the same instance as `this`. What you're trying to do doesn't make sense. – shmosel Jan 30 '19 at 01:29
  • 4
    `super` is not 'the reference to the parent class'. It is a keyword which allows you to refer to an inherited field or method. Nothing more. – user207421 Jan 30 '19 at 01:33

3 Answers3

4

Use of Super Keyword:

  1. super() can be used to refer immediate parent class instance variable.
  2. super() can be used to invoke immediate parent class method.
  3. super() can be used to invoke immediate parent class constructor.

Your compiler is a compilation error for (A)super).x , because it is not a valid statement, moreover we don't use it this way, ontop of all It violates encapsulation, you should not able to bypass parent class. In every definition of super() you will find something mentioned as current parent class, but what you are trying to do here is bypassing current parent.

Now coming to your problem:

x                // Field x in class C
this.x           // Field x in class C
super.x          // Field x in class B
((B)this).x      // Field x in class B
((A)this).x      // Field x in class A
super.super.x    // Illegal; does not refer to x in class A
((A)super).x     // Illegal as well as compilation error

If you still want to access variables like what you intented then use something like below:

t1.x              // Field x of class C 
((B)t1).x         // Field x of class B
((A)t1).x         // Field x of class A

Note: t1 is your class C instance.

Vishwa Ratna
  • 5,567
  • 5
  • 33
  • 55
  • 3
    ```super()``` and ```super``` are rather different things. The former is a constructor call, the latter operates so as to modify the usual name resolution algorithm. –  Jan 30 '19 at 02:33
0

The answer to why this can be cast but super cannot is the same answer to the question why this can be passed as a method argument but super cannot: namely, because this is defined by the JLS as a Primary Expression but super is not.

jaco0646
  • 15,303
  • 7
  • 59
  • 83
  • Upvote for the (indirect) pointer to the language spec; section 15.11.2 in the JLS for Java SE 11, for anyone else interested. –  Jan 30 '19 at 13:30
-1

The super keyword refers to a class parent, and this keyword refers to the class you are in. Let's create a parent class to start the demonstration:

public class ParentClass{

  private int justANumber;

  public ParentClass(int justANumber){
    this.justANumber = justANumber;
  }

}

Notice how the this keyword is used here, which is telling "Hey, assign the justANumber value to THIS class attribute also called justANumber". Let's create a subclass for this parent class now:

public class Subclass extends ParentClass{

  // You don't need to declare the justANumber variable, cause it's from the parent

  public Subclass(int justANumber){
    super(justANumber);
  }

  public showNumber(){
    return this.justANumber;
  }

}

The super() method calls the parent constructor, which need an int value on the first place, so you pass it as an argument. Now see how the showNumber() method returns this.justANumber? Why? that's because when the super() method is called, the parent class automatically delegates his variables to the subclass, so in this case, Subclass can now say that justANumber is HIS variable, being able to use the this keyword. Hope you now understand de difference.

Christian
  • 24
  • 1
  • 3
  • 1
    The original post does not use the superclass constructor call ```super()``` anywhere. It uses, and is asking about, the superclass access keyword ```super```. –  Jan 30 '19 at 03:28