Which keyword can be used to refer to the parent classes methods and fields?
The 5 keyword is used to access properties on an object literal or class's [[Prototype]], or invoke a superclass's constructor. Show
The 6 and 7 expressions are valid in any method definition in both classes and object literals. The 8 expression is valid in class constructors.Syntax
DescriptionThe 5 keyword can be used in two ways: as a "function call" ( 8), or as a "property lookup" ( 6 and 7).Note: 5 is a keyword and these are special syntactic constructs. 5 is not a variable that points to the prototype object. Attempting to read 5 itself is a 6.
In the constructor body of a derived class (with 7), the 5 keyword may appear as a "function call" ( 8), which must be called before the 0 keyword is used, and before the constructor returns. It calls the parent class's constructor and binds the parent class's public fields, after which the derived class's constructor can further access and modify 0.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 5 can be either the superclass's constructor itself, or the constructor's 3, depending on whether the execution context is instance creation or class initialization. See the Examples section for more details.Note that the reference of 5 is determined by the class or object literal 5 was declared in, not the object the method is called on. Therefore, unbinding or re-binding a method doesn't change the reference of 5 in it (although they do change the reference of 0). You can see 5 as a variable in the class or object literal scope, which the methods create a closure over. (But also beware that it's not actually not a variable, as explained above.)When setting properties through 5, the property is set on 0 instead.ExamplesUsing super in classesThis code snippet is taken from the classes sample (live demo). Here 1 is called to avoid duplicating the constructor parts' that are common between 2 and 3.
Super-calling static methodsYou are also able to call super on static methods.
Accessing super in class field declaration 5 can also be accessed during class field initialization. The reference of 5 depends on whether the current field is an instance field or a static field.
Note that instance fields are set on the instance instead of the constructor's 3, so you can't use 5 to access the instance field of a superclass.
Here, 8 is 9 instead of 10, because 0 is defined as an own property of the 1 instance, instead of 2. 5, in this context, only looks up properties on 2, because that's the [[Prototype]] of 5.Deleting super properties will throw an errorYou cannot use the 6 operator and 6 or 7 to delete a parent class' property — it will throw a 9.
Using super.prop in object literalsSuper can also be used in the object initializer notation. In this example, two objects define a method. In the second object, 5 calls the first object's method. This works with the help of 1 with which we are able to set the prototype of 2 to 3, so that 5 is able to find 5 on 3.
Methods that read super.prop do not behave differently when bound to other objectsAccessing 7 behaves like 8, which means the property is always seeked on the object literal/class declaration's prototype, and unbinding and re-binding a method won't change the reference of 5.
The same happens in object literals.
Only resetting the entire inheritance chain will change the reference of 5. 0Calling methods from superWhen calling 6 as a function, the 0 value inside the 3 function is the current 0, not the object that 5 points to. For example, the 6 call logs 7, despite the code looking like it's equivalent to 8. 1This is especially important when interacting with static private properties. Setting super.prop sets the property on this insteadSetting properties of 5, such as 0, behaves like 1. This is one of the cases where understanding 5 as simply "reference of the prototype object" falls short, because it actually sets the property on 0 instead. 2 0 will look for the property descriptor of 5 on 6 (and invoke the setters defined there), but the 0 value will be set to 0, which is 9 in this context. You can read 0 for more details on the case when 1 and 2 differ.This means that while methods that get 6 are usually not susceptible to changes in the 0 context, those that set 6 are. 3However, 0 still consults the property descriptor of the prototype object, which means you cannot rewrite non-writable properties, and setters will be invoked.
Which keyword is used to call parent class constructor and method from within?The super keyword is used to call the constructor of its parent class to access the parent's properties and methods.
What is the keyword used to refer to the parent object?The super keyword refers to superclass (parent) objects. It is used to call superclass methods, and to access the superclass constructor. The most common use of the super keyword is to eliminate the confusion between superclasses and subclasses that have methods with the same name.
Which of the following keyword are used to invoke a method in the parent class?Invoking base class constructor
The super keyword can also be used to invoke the parent class constructor, both parameterized and empty, in the derived class.
Which keyword is used to call methods of the parent class in Java?The super keyword is used to explicitly call the no-arg and parameterized constructor of the parent class. The super keyword is used to access the method of parent class when the child class has overridden that method.
|