Last Updated on March 21, 2023 by Prepbytes
Java is one of the most popular programming languages used in various domains, including web development, Android app development, and desktop application development. It is an object-oriented language that allows developers to create reusable and modular code. One of the essential features of Java is the ‘super’ keyword, which plays a significant role in inheritance. In this article, we will discuss the super keyword in Java, its syntax, and its usage in various scenarios.
Super Keyword in Java
The super keyword in Java is a reference variable that is used to refer to the immediate parent class’s object. It is used to access the members of the parent class, including fields, constructors, and methods. The super keyword in Java is primarily used in inheritance, where a child class inherits properties and behavior from the parent class.
Syntax of Super Keyword in Java
The super keyword in Java is used for various different purposes.
The syntax for different uses is given below.
-
The super Keyword in Java is used to refer to the object that is present in the immediate parent class. The syntax is:
super.member;
Here ‘member’ can be any member of the parent class, including fields, constructors, and methods.
-
The super keyword in Java is used to call the parent class’s constructor, using the following syntax:
super(args);
Here ‘args’ are the arguments passed to the parent class’s constructor.
-
The super keyword in Java can also be used to call the parent class’s method, using the following syntax:
super.method(args);
Where ‘method’ is the method of the parent class, and ‘args’ are the arguments passed to the method.
Examples of Usage of Super Keyword in Java
The following examples demonstrate the usage of the super Keyword in Java.
Example 1 of Super Keyword in Java: Accessing the Parent Class’s Field
As mentioned above, The super keyword in Java can be used to access the parent class’s field from the child class. When a child class inherits a field from the parent class, it can access it using the super keyword in Java. Here is an example:
Code:
class Parent { int num = 10; } class Child extends Parent { int num = 20; void display() { System.out.println("Child Class Num: " + num); System.out.println("Parent Class Num: " + super.num); } } public class Main { public static void main(String[] args) { Child c = new Child(); c.display(); } }
Output:
Child Class Num: 20
Parent Class Num: 10
Explanation:
In this example, the ‘Parent’ class has a field ‘num’ with a value of 10. The ‘Child’ class also has a field ‘num’ with a value of 20. The ‘display’ method of the ‘Child’ class uses the ‘super’ keyword to access the parent class’s ‘num’ field. And we get the expected output on the console screen which shows “Child Class Num: 20” in the first line and “Parent Class Num: 10” in a new line. Here, the Child class is directly accessing the value of variable num present in the Parent Class using the super Keyword in Java.
Example 2 of Super Keyword in Java: Accessing the Parent Class’s Constructor
The super keyword in Java can be used to call the parent class’s constructor from the child class’s constructor. When a child class extends a parent class, it must call the parent class’s constructor before executing its own constructor. Here is an example that demonstrates the above example.
Code:
class Parent { Parent() { System.out.println("Parent Constructor"); } } class Child extends Parent { Child() { super(); System.out.println("Child Constructor"); } } public class Main { public static void main(String[] args) { Child c = new Child(); } }
Output:
Parent Constructor
Child Constructor
Explanation:
In this example, the ‘Child’ class extends the ‘Parent’ class. The ‘Parent’ class has a constructor that prints "Parent Constructor" when called. The ‘Child’ class also has a constructor that calls the parent class’s constructor using the ‘super’ keyword and prints "Child Constructor".
Example 3 of Super Keyword in Java: Calling the Parents’ Class Method from Child’s Class Overridden Method
The ‘super’ keyword can be used to call the parent class’s method from the child class’s overridden method. When a child class overrides a parent class’s method, it can call the parent class’s method using the ‘super’ keyword. Here is an example:
Code:
class Parent { void display() { System.out.println("Parent Method"); } } class Child extends Parent { void display() { super.display(); System.out.println("Child Method"); } } public class Main { public static void main(String[] args) { Child c = new Child(); c.display(); } }
Output:
Parent Method
Child Method
Explanation:
In this example, the ‘Child’ class overrides the parent class’s ‘display’ method. So, when we call the display() method from the main class on the object of class “Child”. The control gifts to the display() method of the Child’s class. Since we have used the super Keyword in Java, it calls the method present in the Parent Class, and then prints “Parent Method” on the console screen. After complete execution of this, the control return to the display() method of the Child class and prints the “Child Method” in a new line on the screen.
Advantages of Super Keyword in Java
The usage of super Keyword in Java provides several advantages. Some of them are listed below:
- Allows for code reuse: Using the super keyword in Java allows subclasses to inherit functionality from their parent classes, increasing code reuse and reducing duplication.
- Enables Polymorphism: Polymorphism is possible since subclasses can override methods and access fields from their parent classes through the usage of the super keyword in Java. This allows more flexible and scalable code.
- Provides access to the Parent’s Class Method: Subclasses can utilize the super keyword to access and use methods and fields defined in their parent classes, allowing them to make use of existing behavior without having to reimplement it.
- Promotes usage of encapsulation and abstraction: The usage of super Keyword in Java promotes encapsulation and abstraction by allowing subclasses to focus on their own behavior while relying on the parent class for handling the lower-level details.
Conclusion
In object-oriented programming, inheritance is a powerful feature that allows a child class to inherit properties and behavior from the parent class. The ‘super’ keyword in Java is important in inheritance because it allows a child class to access the parent class’s members, such as fields, constructors, and methods. In this article, we learned the syntax and usage of the super Keyword in Java. By using the super keyword in Java effectively, developers can create reusable and modular code that is easy to maintain and extend.
Frequently Asked Questions (FAQs)
Some Frequently Asked Questions on the super Keyword in Java are listed below.
Ques 1. Can we use the super keyword in Java to access private members of the parent class?
Ans. No, the super keyword in Java cannot be used to access private members of the parent class. Private members are not visible to child classes.
Ques 2. Can the super keyword in Java be used in a static method?
Ans. No, the super keyword in Java cannot be used in a static method because it refers to the parent class’s instance, which is not applicable in a static context.
Ques 3. What happens if the super keyword in Java is not used in a constructor?
Ans. If the ‘super’ keyword is not used in a constructor in Java, the compiler automatically inserts a call to the default constructor of the parent class.
Ques 4. Can the super keyword in Java be used to override a method?
Ans. No, the super keyword in Java cannot be used to override a method. It is used to call the parent class’s method from the child class’s method.