According to The Java Language Specification, §12.5, "Creation of New Class Instances" [JLS 2015]: Show
Invocation of an overridable method during object construction may result in the use of uninitialized data, leading to runtime exceptions or to unanticipated outcomes. Calling overridable methods from constructors can also leak the Noncompliant Code ExampleThis noncompliant code example results in the use of uninitialized data by the class SuperClass { public SuperClass () { doLogic(); } public void doLogic() { System.out.println("This is superclass!"); } } class SubClass extends SuperClass { private String color = "red"; public void doLogic() { System.out.println("This is subclass! The color is :" + color); // ... } } public class Overridable { public static void main(String[] args) { SuperClass bc = new SuperClass(); // Prints "This is superclass!" SuperClass sc = new SubClass(); // Prints "This is subclass! The color is :null" } } The Compliant SolutionThis compliant solution declares the class SuperClass { public SuperClass() { doLogic(); } public final void doLogic() { System.out.println("This is superclass!"); } } Risk AssessmentAllowing a constructor to call overridable methods can provide an attacker with access to the
Automated DetectionAutomated detection of constructors that contain invocations of overridable methods is straightforward. BibliographyThe super keyword is used to access properties on an object literal or class's [[Prototype]], or invoke a superclass's constructor. The Syntax
DescriptionThe Note:
In the constructor body of a derived
class (with The "property lookup" form can be used to access methods and properties of an object literal's or class's [[Prototype]]. Within a class's body,
the reference of Note that the reference of When setting properties through ExamplesUsing super in classesThis code
snippet is taken from the classes sample (live demo). Here
Super-calling static methodsYou are also able to call super on static methods.
Accessing super in class field declaration
Note that instance fields are set on the instance instead of the constructor's
Here, Deleting super properties will throw an error You cannot use the
delete operator and
Using super.prop in object literalsSuper can also be used in the object initializer / literal notation. In this example, two objects define a method. In the
second object,
Methods that read super.prop do not behave differently when bound to other objectsAccessing
The same happens in object literals.
Only resetting the entire inheritance chain will change the reference of
Setting super.prop will set the property on this insteadSetting properties of
This means that while methods that get
However,
Specifications
Browser compatibilityBCD tables only load in the browser See alsoHow does a subclass invoke its superclass constructor?A subclass can call a constructor defined by its superclass by use of the following form of super: super(parameter-list); Here, parameter-list specifies any parameters needed by the constructor in the superclass. super( ) must always be the first statement executed inside a subclass constructor.
Which keyword is used to refer super class constructor from subclass constructor?For super keyword in Java, we have the base keyword in C#. Super keyword in Java refers immediate parent class instance. It is used to differentiate the members of superclass from the members of subclass, if they have same names. It is used to invoke the superclass constructor from subclass.
When you create an object of a subclass the constructor of the superclass calls the constructor of the subclass True or false?(*2) A subclass constructor always calls the constructor of its superclass, and so on up to the Object constructor, to initialize all the aspects of the instance that the subclass inherits from its superclass.
Can a subclass use a superclass constructor?A subclass inherits all the members (fields, methods, and nested classes) from its superclass. Constructors are not members, so they are not inherited by subclasses, but the constructor of the superclass can be invoked from the subclass.
|