- Super Keyword in Java
- Use of Super Keyword
- How to Use Super Keyword With Variables
- How to Use Super Keywords with Method
- How to Use Super Keywords with Constructor
- How to Call the Parent Class parametrized Constructor from Child Class.
- Important Point About Super() In Constructor
- What is super keywords in Java?
- What is the difference between this() and super() in Java?
- What is the use of the super keyword in interface Java?
- Using the Keyword super
- Subclass Constructors
- What is super interface in java
- 2) super can be used to invoke parent class method
- 3) super is used to invoke parent class constructor.
- Note: super() is added in each class constructor automatically by compiler if there is no super() or this().
- super example: real use
- Feedback
- Help Others, Please Share
- Learn Latest Tutorials
- Preparation
- Trending Technologies
- B.Tech / MCA
- Javatpoint Services
- Training For College Campus
Super Keyword in Java
Super Keyword in Java: The Keyword Super refers to the immediate parent class object. But before we learn about a super keyword and how to use the super keyword in Java programming language, we should know inheritance. If you don’t know about inheritance, follow our Inheritance Java tutorial guide here.
The “super” keyword is used to refer to the superclass of a class. It is commonly used to access methods, fields, and constructors of the parent class from the child class. The “super” keyword is used to avoid naming conflicts between the parent and child class and enable the reuse of code.
Use of Super Keyword
- You Can access the data member of the parent class using the super keyword when the same data member is present in both parent and child classes.
- You Can call the parent class no-arg constructor or parametrized constructor using the Super keyword in the child class.
- When you have to override a parent class method in the child class, but you want to call the parent class method, that time, you need to use the super keyword for access to the parent class method.
Let’s discuss how to use super keywords with variables, methods & constructors with simple examples so that we can understand them easily.
How to Use Super Keyword With Variables
Both parent and child classes have the same variable or data member in your program. In this case, there is a chance of ambiguity for JVM. So to access the parent class variable from the child class, we can use the super keyword. Let’s go through a program so that you can understand how the super keyword works with the variables.
package com.SoftwareTestingo.JavaBasics; class Parent < //Super Class Variable int var=111; >public class Super_Keyword_With_Variable extends Parent < //Child Class Variable int var=222; public void printSuperVar() < System.out.println("Parent Class Var Value: "+super.var); >public static void main(String[] args) < Super_Keyword_With_Variable obj=new Super_Keyword_With_Variable(); System.out.println("The Value Of Local Variable: " + obj.var); obj.printSuperVar(); >>
The Value Of Local Variable: 222 Parent Class Var Value: 111
How to Use Super Keywords with Method
When you want to call the parent class method from the child class, where the parent and child class have the same method name, this type of situation is called method overriding. But there is a chance of ambiguity for the JVM because, in the child class, it has both parent and child class methods with the same name, so to resolve the ambiguity situation, we can use the super keyword.
Go through the below example program to understand clearly without any confusion.
package com.SoftwareTestingo.JavaBasics; class PClass < void show() < System.out.println("Parent Class Show Method Called"); >> public class Super_Keyword_With_Method extends PClass < void show() < System.out.println("Child Class Show Method Called"); >void callShowMethod() < show(); super.show(); >public static void main(String[] args) < Super_Keyword_With_Method obj=new Super_Keyword_With_Method(); obj.callShowMethod(); >>
Child Class Show Method Called Parent Class Show Method Called
Note: When the child class is not overriding the parent class methods, that time no need to use the super keyword to access the parent class methods.
How to Use Super Keywords with Constructor
When creating the object of the child class using the new keyword, that time it calls the child class default constructor. The child class constructor indirectly calls the parent class constructor. So The order of execution of the construct is from parent class to child class.
It all happens because the JVM compiler automatically adds the super() statement at the beginning of the child class construct. So the super() statement calls the no-args constructor of the parent class. In this way, the constructor execution comes from the parent class constructor to the child class constructor.
Let’s go through with an example to understand how the child class constructor calls the parent class constructor.
package com.SoftwareTestingo.JavaBasics; class ParentClass < public ParentClass() < System.out.println("Parent Class No Arg Consructor Executed"); >public ParentClass(String abc) < System.out.println("Parent Class With Argument Consructor Executed"); >> public class Super_Keyword_With_Constructor extends ParentClass < public Super_Keyword_With_Constructor() < //super(); super("Welcome"); System.out.println("Child Class Constructor Got Executed"); >public static void main(String[] args) < Super_Keyword_With_Constructor obj=new Super_Keyword_With_Constructor(); System.out.println("Main Method executed"); >>
Parent Class With Argument Consructor Executed Child Class Constructor Got Executed Main Method executed
How to Call the Parent Class parametrized Constructor from Child Class.
But If our requirement is called the parent class parameterized constructor, we can also achieve that by using the parametrized super() statement inside the child class constructor.
Go through with the below Example program:
package com.SoftwareTestingo.JavaBasics; class Super_Parent_Class < Super_Parent_Class() < System.out.println("Parent Class Default Constructor"); >Super_Parent_Class(String str) < System.out.println(str +": This is Parametrized Constructor Of Parent Class"); >> public class Super_Keyword_With_Param_Constructor extends Super_Parent_Class < Super_Keyword_With_Param_Constructor() < super("User"); System.out.println("Child Class Default Constructor"); >public void display() < System.out.println("Simply Printing Hello"); >public static void main(String[] args) < Super_Keyword_With_Param_Constructor obj=new Super_Keyword_With_Param_Constructor(); obj.display(); >>
User: This is Parametrized Constructor Of Parent Class Child Class Default Constructor Simply Printing Hello
Note: The Super() should be the first statement inside the method where you want to call the parent constructor.
Important Point About Super() In Constructor
- When using a super() statement in the child class constructor, that time the super() statement should be the first statement of the child class. Otherwise, you will get the compilation error: “Constructor call must be the first statement in a constructor”.
- If you have not added any super() statement inside the child class constructor at that time Java JVM compiler automatically adds a super() statement at the beginning of the child constructor; if the parent class doesn’t have any no-argument constructor at that time, you will get a compile-time error.
- When you mention the super() statement inside the child class constructor that time the JVM compiler doesn’t call the parent class no-args constructor.
If you find any mistake or want to add more information to this post, comment in the comment section.
What is super keywords in Java?
The “super” keyword is used to refer to the superclass of a class. It is used to access methods, fields, and constructors of the parent class from the child class and avoid naming conflicts between the parent and child. It is commonly used to call the constructor of the parent class, access methods and fields of the parent class, and call a constructor or method of the parent class from within the constructor or method of the child class.
What is the difference between this() and super() in Java?
we use “This()” to invoke a constructor within the same class, but “super()” is used to invoke a constructor in the superclass. “This” refers to the current instance of the class, while “super” refers to the parent class.
What is the use of the super keyword in interface Java?
the use of the “super” keyword is not possible in interfaces because interfaces do not have parent classes. Nonetheless, when an interface extends another interface, the “super” keyword can be used to access the methods of the parent interface.
Using the Keyword super
If your method overrides one of its superclass’s methods, you can invoke the overridden method through the use of the keyword super . You can also use super to refer to a hidden field (although hiding fields is discouraged). Consider this class, Superclass :
Here is a subclass, called Subclass , that overrides printMethod() :
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 Subclass , the simple name printMethod() refers to the one declared in Subclass , which overrides the one in Superclass . So, to refer to printMethod() inherited from Superclass , Subclass must use a qualified name, using super as shown. Compiling and executing Subclass prints the following:
Printed in Superclass. Printed in Subclass
Subclass Constructors
The following example illustrates how to use the super keyword to invoke a superclass’s constructor. Recall from the Bicycle example that MountainBike is a subclass of Bicycle . Here is the MountainBike (subclass) constructor that calls the superclass constructor and then adds initialization code of its own:
public MountainBike(int startHeight, int startCadence, int startSpeed, int startGear)
Invocation of a superclass constructor must be the first line in the subclass constructor.
The syntax for calling a superclass constructor is
With super() , the superclass no-argument constructor is called. With super(parameter list) , the superclass constructor with a matching parameter list is called.
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. Object does have such a constructor, so if Object is the only superclass, there is no problem.
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 Object . In fact, this is the case. It is called constructor chaining, and you need to be aware of it when there is a long line of class descent.
What is super interface in java
2) super can be used to invoke parent class method
The super keyword can also be used to invoke parent class method. It should be used if subclass contains the same method as parent class. In other words, it is used if method is overridden.
In the above example Animal and Dog both classes have eat() method if we call eat() method from Dog class, it will call the eat() method of Dog class by default because priority is given to local.
To call the parent class method, we need to use super keyword.
3) super is used to invoke parent class constructor.
The super keyword can also be used to invoke the parent class constructor. Let’s see a simple example:
animal is created dog is created
Note: super() is added in each class constructor automatically by compiler if there is no super() or this().
As we know well that default constructor is provided by compiler automatically if there is no constructor. But, it also adds super() as the first statement.
Another example of super keyword where super() is provided by the compiler implicitly.
animal is created dog is created
super example: real use
Let’s see the real use of super keyword. Here, Emp class inherits Person class so all the properties of Person will be inherited to Emp by default. To initialize all the property, we are using parent class constructor from child class. In such way, we are reusing the parent class constructor.
For Videos Join Our Youtube Channel: Join Now
Feedback
Help Others, Please Share
Learn Latest Tutorials
Preparation
Trending Technologies
B.Tech / MCA
Javatpoint Services
JavaTpoint offers too many high quality services. Mail us on h[email protected], to get more information about given services.
- Website Designing
- Website Development
- Java Development
- PHP Development
- WordPress
- Graphic Designing
- Logo
- Digital Marketing
- On Page and Off Page SEO
- PPC
- Content Development
- Corporate Training
- Classroom and Online Training
- Data Entry
Training For College Campus
JavaTpoint offers college campus training on Core Java, Advance Java, .Net, Android, Hadoop, PHP, Web Technology and Python. Please mail your requirement at [email protected].
Duration: 1 week to 2 week
Like/Subscribe us for latest updates or newsletter