Introduction
In 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.
The ‘super’ keyword allows referencing the parent class or superclass of a subclass in Java. It is often employed to access members (fields or methods) of the superclass that have been overridden in the subclass. You can call the superclass’s method from within the subclass using super.methodName(). Additionally, super() is used to call the constructor of the superclass from the subclass constructor, which is essential for initializing inherited members. In short, if any programmers want to maintain inheritance hierarchies and enable the reuse of code in object-oriented programming, this super keyword is crucial.
Characteristics of Super Keyword in Java
In Java, super keyword is used to refer to the parent class of a subclass. Here are some of its key characteristics:
1.super is used to call a superclass constructor—When a subclass is created, its constructor must call the constructor of its parent class. This is done using the super() keyword, which calls the constructor of the parent class.
Example
class Superclass {
int num;
// Superclass constructor
Superclass(int num) {
this.num = num;
}
void display() {
System.out.println("Number in superclass: " + num);
}
}
class Subclass extends Superclass {
// Subclass constructor
Subclass(int num) {
super(num); // Calling superclass constructor
}
void display() {
System.out.println("Number in subclass: " + num);
}
}
public class Main {
public static void main(String[] args) {
Subclass obj = new Subclass(10);
obj.display(); // This will call the display method of Subclass
}
}
JavaOutput
Number in subclass: 10
Java2. super is used to call a superclass method: A subclass can call a method defined in its parent class using the super keyword. This is useful when the subclass wants to invoke the parent class’s implementation of the method in addition to its own.
Example
class Superclass {
void display() {
System.out.println("This is the superclass display method");
}
}
class Subclass extends Superclass {
// Subclass method overriding the superclass method
void display() {
super.display(); // Calling superclass method using super keyword
System.out.println("This is the subclass display method");
}
}
public class Main {
public static void main(String[] args) {
Subclass obj = new Subclass();
obj.display(); // This will call the display method of Subclass
}
}
JavaOutput
This is the superclass display method
This is the subclass display method
Java3.super is used to access a superclass field: A subclass can access a field defined in its parent class using the super keyword. This is useful when the subclass wants to reference the parent class’s version of a field.
Example
class Superclass {
int num = 10;
}
class Subclass extends Superclass {
void display() {
System.out.println("Value of num in superclass: " + super.num); // Accessing superclass field using super keyword
}
}
public class Main {
public static void main(String[] args) {
Subclass obj = new Subclass();
obj.display(); // This will display the value of num from the superclass
}
}
JavaOutput
Value of num in superclass: 10
Java4. super must be the first statement in a constructor: When calling a superclass constructor, the super() statement must be the first statement in the constructor of the subclass.
Example
class Superclass {
int num;
// Superclass constructor
Superclass(int num) {
this.num = num;
}
}
class Subclass extends Superclass {
int anotherNum;
// Subclass constructor
Subclass(int num, int anotherNum) {
super(num); // Calling superclass constructor - must be the first statement
this.anotherNum = anotherNum;
}
void display() {
System.out.println("Number in superclass: " + num);
System.out.println("Another number in subclass: " + anotherNum);
}
}
public class Main {
public static void main(String[] args) {
Subclass obj = new Subclass(10, 20);
obj.display(); // This will display the numbers from both superclass and subclass
}
}
JavaOutput
Number in superclass: 10
Another number in subclass: 20
Java5.super cannot be used in a static context: The super keyword cannot be used in a static context, such as in a static method or a static variable initializer.
Example
class Superclass {
static int num = 10;
}
class Subclass extends Superclass {
static void display() {
// This will result in a compilation error because super cannot be used in a static context
// System.out.println("Value of num in superclass: " + super.num);
}
}
public class Main {
public static void main(String[] args) {
Subclass.display(); // Calling the static method of Subclass
}
}
JavaOutput
// This will result in a compilation error because super cannot be used in a static context
System.out.println("Value of num in superclass: " + Superclass.num);
Java6. super is not required to call a superclass method: While it is possible to use the super keyword to call a method in the parent class, it is not required. If a method is not overridden in the subclass, then calling it without the super keyword will invoke the parent class’s implementation.
Example
class Superclass {
void display() {
System.out.println("This is the superclass display method");
}
}
class Subclass extends Superclass {
// Subclass method overriding the superclass method
void display() {
// Calling superclass method without using super keyword
super.display();
System.out.println("This is the subclass display method");
}
}
public class Main {
public static void main(String[] args) {
Subclass obj = new Subclass();
obj.display(); // This will call the overridden display method of Subclass
}
}
JavaOutput
This is the superclass display method
This is the subclass display method
JavaUse of Super Keyword in Java
It is majorly used in the following contexts as mentioned below:
- Use of super with Variables
- Use of super with Methods
- Use of super with Constructors
Example
class Superclass {
int num = 10;
void display() {
System.out.println("This is the superclass display method");
}
// Superclass constructor
Superclass() {
System.out.println("This is the superclass constructor");
}
}
class Subclass extends Superclass {
int anotherNum = 20;
// Subclass constructor
Subclass() {
super(); // Calling superclass constructor
System.out.println("This is the subclass constructor");
}
void display() {
super.display(); // Calling superclass method
System.out.println("This is the subclass display method");
}
void accessVariables() {
System.out.println("Value of num in superclass: " + super.num); // Accessing superclass variable using super keyword
System.out.println("Value of anotherNum in subclass: " + anotherNum);
}
}
public class Main {
public static void main(String[] args) {
Subclass obj = new Subclass();
obj.display(); // This will call the display method of Subclass
obj.accessVariables(); // This will access the variables from both superclass and subclass
}
}
JavaOutput
This is the superclass constructor
This is the subclass constructor
This is the superclass display method
This is the subclass display method
Value of num in superclass: 10
Value of anotherNum in subclass: 20
JavaAdvantages of Using Java Super Keyword
Using the super
keyword in Java offers several advantages, including:
- Accessing superclass members: Allows accessing members (variables, methods, and constructors) of the superclass from within the subclass.
- Method overriding: Facilitates calling overridden methods of the superclass from within the subclass, enabling the extension of functionality without losing the original behavior.
- Constructor chaining: Enables calling the constructor of the superclass from within the subclass constructor, allowing initialization of inherited members and ensuring proper initialization of the superclass.
- Clarity and readability: Enhances code clarity and readability by explicitly indicating the invocation of superclass members or constructors, improving code comprehension for developers.
- Avoiding ambiguity: Helps to distinguish between superclass and subclass members with the same name, ensuring clarity in cases of shadowing or overriding.
- Maintaining code consistency: Promotes consistent usage of superclass members across subclasses, facilitating code maintenance and reducing the likelihood of errors.
Conclusion
- The super keyword in Java is used to access the members of the immediate parent class.
- There are three main uses of the super keyword in Java:
- To access the data members of the immediate parent class
- To invoke the overridden methods of the immediate parent class
- To call both default and parameterized constructors of the immediate parent class
- The Java compiler implicitly calls the no-arg constructor of the parent class in case a call to the super() is not made in the constructor of the child class.
- We don’t need the super keyword to access non-overridden members and methods of the parent class.
Frequently Asked Questions
super
keyword in Java? The super
keyword in Java is a reference variable that is used to access members of the superclass within the subclass. It can access variables, methods, and constructors of the superclass.
super
keyword? You should use the super
keyword when you want to access superclass members (variables, methods, constructors) within a subclass. It’s particularly useful for invoking superclass constructors, accessing overridden methods, or accessing superclass variables with the same name as subclass variables.
super
keyword work with constructors? In constructors of a subclass, the super()
call must be the first statement if it’s used. It is used to call the constructor of the superclass, ensuring that the superclass constructor is executed before the subclass constructor.
super
keyword in static methods? No, the super
keyword cannot be used in static methods or any other static context. It is only applicable within non-static contexts such as instance methods or constructors.
super()
in a subclass constructor? If you don’t explicitly call super()
in a subclass constructor, the compiler will automatically insert a call to the no-argument constructor of the superclass. If the superclass doesn’t have a no-argument constructor and there’s no explicit call to a superclass constructor, a compilation error will occur.