Java does not allow you to use super
as a value. You cannot return it. And you don’t need to return it, because you can reference the object with this
.
In Java, an instance of class is represented by one object. The superclass is not represented by a separate object.
Classes support inheritance of implementation from a superclass. This allows us to write common code that be used or overridden by subclasses.
Superclasses and interfaces support polymorphism. For example, a reference of a superclass type can be used to call methods from a subclass object. This allows us to exploit the Liskov Substitution Principle to write code once that can handle multiple types. This helps us not repeat ourselves. From the Java Language Specification, section 1.1: 1.1 Organization of the Specification:
Classes support single implementation inheritance, in which the implementation of each class is derived from that of a single superclass … Variables of a class type can reference an instance of that class or of any subclass of that class, allowing new types to be used with existing methods, polymorphically.
Java does allow you to refer to the superclass with the keyword super
— but only to:
- Call a superclass constructor with arguments
- Access members of the superclass, from within the subclass.
Details
From the Java Language Specification, section 8.8.7.1, Explicit constructor invocations
Superclass constructor invocations begin with either the keyword super (possibly prefaced with explicit type arguments) or a Primary expression or an ExpressionName. They are used to invoke a constructor of the direct superclass.
From the Java Language Specification, section 15.11.2, Accessing Superclass Members using super
:
The form super.Identifier refers to the field named Identifier of the current object, but with the current object viewed as an instance of the superclass of the current class.
So super
is a keyword. But the language does not support using super
as a value.
10
solved Can you return the super in Java/C#? (would it be useful if u could?) [closed]