Accessing Superclass MembersIf your method overrides one of its superclass's methods, you can invoke the overridden method through the use of the keyword Show
public class Superclass { public void printMethod() { System.out.println("Printed in Superclass."); } } Here is a subclass, called public class Subclass extends Superclass { // overrides printMethod in Superclass public void printMethod() { super.printMethod(); System.out.println("Printed in Subclass"); } public static void main(String[] args) { Subclass s = new Subclass(); s.printMethod(); } } Within Printed in Superclass. Printed in Subclass Subclass ConstructorsThe following example illustrates how to use the public MountainBike(int startHeight, int startCadence, int startSpeed, int startGear) { super(startCadence, startSpeed, startGear); seatHeight = startHeight; } Invocation of a superclass constructor must be the first line in the subclass constructor. The syntax for calling a superclass constructor is or: With Note: If a constructor does not explicitly invoke a superclass constructor, the Java compiler automatically inserts a call to the no-argument constructor of the superclass. If the super class does not have a no-argument constructor, you will get a compile-time error. If a subclass constructor invokes a
constructor of its superclass, either explicitly or implicitly, you might think that there will be a whole chain of constructors called, all the way back to the constructor of Learn about This and Super Keyword in Java. The this and super are reserved keywords in Java. The super keyword is used to represent an instance of the parent class and can invoke constructor of the parent class durning inheritance, whereas the this keyword is used to represent an instance of the current class, also this keyword is used to refer the static members as well as invoke
constructor of the current class. Java provides many implicit keywords among those “this” and “super” are two reference variables. “this” and “super” are used to resemble the reference variable pointing to sub-class or parent class (superclass). this: this is the reserved keyword in Java that is used to invoke constructor, methods, static members of the current class. super: super is the reserved keyword in Java that is used to invoke constructor, methods of the parent class. This is possible only when one class inherits another class(child class extends parent class). What is this keyword in Java?
Let us under the use of this keyword using the above code snippet. As we can see that we have a variable 'var' and a parameter 'var' inside the method function. If we want to assign the value of parameter 'var' to the variable 'var' inside the class then we have to use the this keyword. In technical terms we are using this keyword tw refer the instance of current class. “this” is a special keyword in Java that is used to refer to the instance of the current class. Using the “this” keyword, we can refer to the current class's methods, constructor, instance variables, and many more. We cannot use the “this” keyword as an identifier in Java(identifier is name given to any method, class, package, etc.). Uses of “this” keyword in Java
Example of this keyword in JavaExample 1The “this” keyword is referring to an instance and a static variable in the same class. By this we can change the values of the variables that are declared inside the class. Also we can differentiate between the variables that are declared inside the classes and methods.
Output:
Explanation: In the above example, the instanceVar is an instance variable, and the staticVar is a static variable inside the class. Both the variables are referred in the "Scaler" method by using the "this" keyword. instanceVar is initialized with value 5, and staticVar is initialized with value 10. The scaler method reinitializes the instanceVar and staticVar using the "this" keyword with 50 and 100, respectively. There are other two variables having same name as static variables. We are able to differentiate between variables inside the class and methods by using this keyword. Example 2The “this” keyword is used to invoke current class method. Instead of calling all the methods of class differently inside the main class we can use this keyword, so that all the methods can be executed using a single call.
Output: Explanation: In the above example, the Illustration class has two methods scaler and name. The scaler method is invoked inside the name method by using the “this” keyword. To call the scaler method, we don't need to write writing obj.scaler() instead we can call it inside name function using this keyword. Example 3The “this” keyword is used to invoke the constructor of the current class. Suppose a class has two constructor one is no argument constructor and another is parameterized constructor. So for invoking both the constructors by a single call is can be done using a this keyword.
Output:
Explanation: In the above example, there are two constructors. The no argument constructor is called while creating the object, and the parameterized constructor is called within the no argument constructor by using the “this” keyword. current class method. Example 4The “this” keyword is used to pass an argument in the method. At industry level it is used in event handlers or at places where we have to provide reference of one class to another class.
Output: Explanation: In the above example, we are passing the “this” keyword as an argument to call the print method in the invoke method. Example 5The “this” keyword is used to pass as an argument in the constructor call. By passing this as an argument in the constructor call we can exhange data from one class to another. This makes exchange of data between multiple classes a lot easier.
Output: Explanation: In the above example, we have passed the "this" keyword as an argument in the constructor call. Here A and B are two classes; parameterized constructor of A is called in class B by creating an object of class A and passing "this" as a parameter. Example 6The “this” keyword is used to return the current class instance. Methods of the class can be called directly at the time of creating an object.
Output: Explanation: In the above example, the getIllustration method returns the current class instance and through which we called the print method. What is a “super” keyword in Java?
Let me explain the term immediate parent. In the above code snippet of multi-level inheritance class B extends class A, it implies class A is immediate parent of class B. Similarly class C extends class B and now class C has two parents i.e., class A and class B, where class B is immediate parent of class C. What is used wants to use the methods of class A in class B? We solve this issue using the super keyword. “super” is a special keyword in Java that is used to refer to the instance of the immediate parent class. Using the “super” keyword, we can refer to the immediate parent class's methods, constructor, instance variables, and many more. We cannot use the “super” keyword as an identifier in Java. Uses of “super” Keyword in Java
Examples of “super” Keyword in JavaExample 1The “super” keyword is used to refer to an instance variable of an immediate parent class.
Output:
Explanation: In the above example, the child class extends the parent class. Both have two instance variables, a and s. In child class, the print method calls the instance variables of the parent class using the "super" keyword. In comparison, the instance variables of child class don't need one. It doesn't matter whether the instance variable of parent class and child class share the same name; they can hold different values and are not overridden. Example 2The “super” keyword is used to invoke an immediate parent class method.
Output:
Explanation: In the above example, child class extends parent class. Both have a display method. The print method in the child class invokes the display method of the parent class by using the “super” keyword. The example says that to invoke the methods in the child class from the parent class, we must use the “super” keyword. Example 3The “super” keyword is used to invoke an immediate parent class constructor.
Output:
Explanation: In the above example, the child class extends parent class. While creating an object of the child class, the constructor of the child class is called, where the parent class constructor is invoked using the “super” keyword. Difference Between this and super Keyword in Java
Similarities Between “this” and “super” Keyword in Java
Example 1
Output:
Explanation: In the above example, we are referencing the non-static variable "this" in the static context. This led to a compiler error.
Example 2
Output:
Explanation: In the above example, we first forward a call from the child class no-argument constructor to the child class parameterized constructor using the "this" keyword. We are forwarding a call to the parent class parameterized constructor using the "super" keyword from a child class parameterized constructor.
Example 3
Output:
Explanation: In the above example, the error will be displayed as shown in output because the "this()" statement is not executed first. Can we Use Both this() and super() in the Same Constructor?No, we cannot use the “this()” and “super()” in the same constructor. If we want to use “this()” or “super()” inside a constructor, they must be written (executed) at first. Both “this()” and “super” cannot be executed simultaneously, hence “this()” and “super()” in Java cannot be used in the same constructor. Important Points About this() and super() in Java
Conclusion.
FAQ’s1. Can we have “this()” and “super()” together?No, we can not use “this()” and “super()” together. 2. Why “this()” and “super()” keywords cannot be used together?“this()” is used to call the constructor of the current class and “super()” is used to call the constructor of the immediate parent class. If a user wants to call a parent or a current class constructor using the “super()” or “this()” keyword, then it must be the first statement inside the constructor, but both “super()” and “this()” cannot be the first statement simultaneously due to this reason “this()” and “super()” keywords cannot be used together. 3. Why do we use super keyword in Java?We use the super keyword to refer to the parent class instance, to invoke parent class methods, static and instance variables. The “super()” is used to invoke the immediate parent class constructor. 4. Why super keyword is used first?Java enforces that the call to super (explicit or not) must be the first statement in the constructor. This is to prevent the subclass part of the object from being initialized before the superclass part of the object being initialized. Which keyword is used to call parent class constructor and method from within the class?The super keyword can also be used to access the parent class constructor.
How do you call the parent class constructor inside the child class?You can call the base class constructor from the child class by using the super() which will execute the constructor of the base class.
Which keyword is used to call constructor of parent?The super keyword can also be used to invoke the parent class constructor.
What Java keyword is used to call the parent constructor from the child constructor?2) Use of super keyword to invoke constructor of parent class. When we create the object of sub class, the new keyword invokes the constructor of child class, which implicitly invokes the constructor of parent class.
|