Last Updated on December 27, 2023 by Ankit Kochar
In the realm of Java programming, the super keyword stands as a powerful and versatile element, offering developers a means to navigate and manipulate class hierarchies. The super keyword is used to refer to the superclass or parent class, providing access to its members and allowing for the invocation of its methods and constructors. This feature is fundamental in scenarios where a subclass inherits from a superclass, allowing for the extension and customization of functionality. This exploration delves into the nuances of the super keyword in Java, uncovering its applications, syntax, and the pivotal role it plays in facilitating efficient and organized object-oriented programming.
Super Keyword in Java
In Java, the super keyword is a reference variable that is used to refer to the immediate parent class object. It is particularly useful in the context of inheritance, where a subclass inherits properties and behaviors from a superclass. The super keyword allows a subclass to access members of its superclass, including fields, methods, and constructors.
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 conclusion, the super keyword in Java serves as a linchpin in object-oriented programming, fostering a seamless relationship between subclasses and their superclasses. By providing a mechanism for accessing and utilizing members of the superclass, super empowers developers to enhance and customize functionality while maintaining the integrity of class hierarchies. Whether invoking superclass methods, accessing fields, or invoking superclass constructors, the super keyword is a valuable tool in the Java developer’s toolkit, contributing to the elegance and efficiency of code.
Frequently Asked Questions (FAQs) related to Super Keyword in Java
Some Frequently Asked Questions on the super Keyword in Java are listed below.
Q1: What is the purpose of the super keyword in Java?
A1: The super keyword in Java is used to refer to the superclass or parent class. It allows access to the members of the superclass, facilitating the invocation of its methods, accessing fields, and invoking its constructors from the subclass.
Q2: How is the super keyword used to call a superclass constructor?
A2: To invoke a superclass constructor using the super keyword, it is placed as the first statement in the constructor of the subclass. This ensures that the superclass constructor is called before any other initialization in the subclass constructor.
Q3: Can the super keyword be used in static methods?
A3: No, the super keyword cannot be used in static methods. It is applicable only in the context of instance methods, constructors, or instance variables within the scope of a non-static context.
Q4: What happens if the super keyword is not explicitly used in a subclass constructor?
A4: If the super keyword is not explicitly used in a subclass constructor, Java automatically inserts a call to the default (parameterless) constructor of the superclass as the first statement in the subclass constructor. If the superclass does not have a default constructor, a compilation error occurs.
Q5: Can the super keyword be used to access private members of the superclass?
A5: No, the super keyword cannot be used to access private members of the superclass directly. It can only access public, protected, or package-private members. If a subclass needs access to private members of the superclass, it must do so through public or protected methods provided by the superclass.