Type Casting the 'super' Keyword in Java

Viewed 307

In Java, it is possible to type cast the 'this' keyword to refer to any of a given class's superclasses. However, trying to do the same with the 'super' keyword results in a syntax error. Why does it not work with the 'super' keyword?

2 Answers

this has reference of object from which method or constructor is called.

super is used to refer parent class variables and methods. In short super keyword cannot be used for up casting.

You can create a method returnThis(){return this;} in parent class and call it in child class to get reference of parent class this for that particular object.

super is used to refer to the super class and doesn't represent any object. this keyword represents the current object from within the this has been called.

super is used in three places:

  1. To refer to super class property when there is a property conflict
  2. to refer super class method in case of overriden methods.
  3. To call parent class constructor.

Please read more about super here

Related