- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What are the uses of super keyword in java?
The super keyword in Java is a reference to the object of the parent/superclass. Using it, you can refer/call a field, a method or, a constructor of the immediate superclass.
Referring to a field using super keyword
As mentioned above, you can refer to an instance filed/variable of the immediate superclass from an instance method or, a constructor of a subclass, using the "super" keyword.
If a class extends another class, a copy of members of the parent class will be created in the subclass i.e. Using the object of the subclass you can access the members of both subclass and the superclass.
If a class has a variable with name same as the variable of the superclass then, you can differentiate the superclass variable from the subclass variable using the super keyword.
Example
In the following Java example, we have two classes SuperClass and SubClass here, the SubClass extends the SuperClass and, both have the same variable "name".
From a method of the class SubClass (display()), we are printing the value of the instance variable (name) of both classes using "super" and "this" keywords respectively −
class SuperClass{ String name = "Raju"; public void display() { System.out.println("This is a method of the superclass"); } } public class SubClass extends SuperClass{ String name = "Ramu"; public void display() { System.out.println("This is a method of the superclass"); System.out.println("Value of name in superclass: "+super.name); System.out.println("Value of name in subclass: "+this.name); } public static void main(String args[]) { new SubClass().display(); } }
Output
This is a method of the superclass Value of name in superclass: Raju Value of name in subclass: Ramu
Referring to a superclass constructor using super keyword
You can also refer/call a constructor of the superclass from the constructor of the child/subclass using the "super" keyword (explicitly).
Example
In the following Java example, we have two classes SuperClass and SubClass. Here, the SubClass extends the SuperClass and both classes have default constructors. We are trying to call the constructor of the superclass from the constructor of the subclass using the super keyword −
class SuperClass{ String name = "Raju"; public SuperClass() { System.out.println("This is the constructor of the superclass"); } } public class SubClass extends SuperClass{ String name = "Ramu"; public SubClass() { System.out.println("This is a constructor of the subclass"); } public static void main(String args[]) { new SubClass(); } }
Output
This is the constructor of the superclass This is a constructor of the subclass
Calling a method using this keyword
You can also use this to call an (instance) method of the superclass from the instance method of the subclass.
Example
In the following Java example, we have two classes SuperClass and SubClass. Here, the SubClass extends the SuperClass.
Here, we are trying to invoke the display() method of the superclass using the super keyword from the subclass’s method named show.
class SuperClass{ String name = "Raju"; public void display() { System.out.println("This is a method of the superclass"); } } public class SubClass extends SuperClass{ String name = "Ramu"; public void show() { System.out.println("This is a method of the subclass"); super.display(); } public static void main(String args[]) { new SubClass().show(); } }
Output
This is the constructor of the superclass This is a constructor of the subclass
- Related Articles
- What are the uses of this keyword in java?
- What are the three usages of the super keyword in Java?
- super keyword in Java
- Equivalent of Java Super Keyword in C#
- Role of super Keyword in Java Inheritance
- Super keyword in JavaScript?
- Super keyword in Dart Programming
- What are the uses of generic collections in Java?
- Golang program to show use of super keyword in class
- Why can't we use the "super" keyword is in a static method in java?
- What is the super class of every class in Java?
- What is the super() construct of a constructor in Java?
- Are ‘this’ and ‘super’ keywords in Java?
- What are the uses of Electromagnets?
- What are the uses of silk?
