The super keyword in Java is a versatile reference variable. It can access objects from the super or parent class, allowing access to parent class constructors, members, and methods in the derived class. Keywords play a crucial role in the inheritance and polymorphism concepts. This article will dive deep into the super keyword and its advantages and disadvantages regarding usage.
What is a super keyword?
The super keyword in object-oriented programming languages, such as Java, refers to the immediate parent class of the current class instance. It serves multiple purposes. It allows access to overridden methods in the parent class. It enables the invocation of the parent class constructor from a child class and facilitates access to the parent class fields that the child class may shadow.
Get curriculum highlights, career paths, industry insights and accelerate your technology journey.
Download brochure
Characteristics of Super Keywords in Java
This section will show some characteristics of super keywords in Java language.
Accessing Parent Class Members: The ‘super’ keyword can access methods or fields of the immediate super or parent class hidden by the subclass created
Super is used to call a superclass constructor: When a subclass is defined in the program, its constructor must call the constructor of its parent class. This is done using the super() keyword, which calls the super or parent class constructor.
Super must be the first statement in a constructor: When calling a superclass constructor, the super() statement must be the first statement in the subclass’s constructor.
Use of super Keyword in Java language
Use of super with Variables
Use of super with Methods
Use of super with Constructors
Use of super with Variables
The ‘super’ keyword is used to access a variable from a parent class when a child has a variable with the same name. It is very helpful because the child variable will overshadow the parents’ variable in the child class. Using the ‘super’ keyword, You can still refer to the parents’ class version.
The following program demonstrates the super keyword example.
Program
class Animal {
int maxSpeed = 120;
}
class Tiger extends Animal {
int maxSpeed = 180;
void display(){
System.out.println("Maximum Speed: "+super.maxSpeed);
}
}
class Main {
public static void main(String[] args){
Tiger small = new Tiger();
small.display();
}
}
Output
Maximum Speed: 120
Use of super with Methods
This condition is when we want to call the parent class method. So whenever a parent and child class have the same-named method, we use the super keyword to resolve ambiguity.
The following program explained everything about the super with methods in Java.
Program
class Person {
void message() {
System.out.println("This is person classn");
}
}
class Student extends Person {
void message() {
System.out.println("This is student class");
}
void display() {
message();
super.message();
}
}
class Main {
public static void main(String args[]) {
Student ob = new Student();
ob.display();
}
}
Output
This is student class
This is person class
Use of super with constructors
We can also use the super keyword to access the superclass constructor, and depending on the situation, we can use super for both parametric and non-parametric constructors.
The following program demonstrates the super keyword.
Program
class Person {
Person(){
System.out.println("Person class Constructor");
}
}
class Student extends Person {
Student(){
super();
System.out.println("Student class Constructor");
}
}
class Main {
public static void main(String[] args){
Student ob = new Student();
}
}
Output
Person class Constructor
Student class Constructor
Program
class SuperClass {
public boolean isTrue() {
return true;
}
}
class SubClass extends SuperClass {
public boolean isTrue() {
boolean parentResult = super.isTrue();
return !parentResult;
}
}
public class Main {
public static void main(String[] args) {
SubClass child = new SubClass();
boolean result = child.isTrue();
System.out.println(result);
}
}
Output
false
Advantages of Super Keyword in Java
The ‘super’ keyword in Java offers several advantages. Let’s discuss each, one by one.
Enable reuse of code: The super keyword allows subclasses to inherit functionality from their parent classes, promoting the reuse of code and reducing duplication.
Supports polymorphism: Because subclasses can override methods and access fields from their parent classes using super keywords, they can take advantage of existing classes without reimplementing it.
Improving Code Readability: Using the ‘super’ keyword clarifies to anyone reading the code that the method or variable accessed belongs to the superclass.
Disadvantages of Super Keyword in Java language
The ‘super’ keyword in Java provides several advantages. It also comes with certain disadvantages. Let’s discuss the disadvantages of super keywords in Java.
Tight Coupling: Using the ‘super’ keyword to call superclass methods or constructors tightly couples the subclass to the superclass. The changes in the superclass implementation can directly affect the subclass, potentially leading to maintenance issues.
Incorrect Method Overriding: If the subclass incorrectly overrides a method, calling ‘super’ can lead to unexpected behaviour. Ensuring methods are properly overridden requires careful attention to method signatures and behaviour.
Leaking Implementation Details: Sometimes, using the ‘super’ keyword can expose the internal workings of the superclass, reducing encapsulation. This can lead to a more fragile design where changes in one class necessitate changes in another.
Conclusion
In this blog, we learned about the super keyword in Java. It is a powerful tool for developers, enabling enhanced control and flexibility in inheritance by allowing access to superclass methods and constructors. It ensures that the subclass can effectively leverage and extend the functionality of its parent class. The ‘super’ keyword can produce more readable, maintainable, and efficient code. It aids in method overriding, constructor chaining, and resolving name conflicts. Understanding and mastering the ‘super’ keyword is essential for any programmer to build robust and scalable applications in Java language.
FAQs
What is the ‘super’ keyword in Java language?
The ‘super’ keyword in Java refers to the current object's immediate parent class. It can be used to access the parent class's methods and variables and to call the parent class constructor.
Can ‘super’ be used as interfaces?
No, ‘super’ cannot be used in interfaces because interfaces do not have constructors or concrete implementations to refer to.
Can the' super' keyword access a hidden file in the parent class?
Yes, ‘super’ can access a hidden file in the parent class. If a field in the child class hides a field in the parent class, You can use ‘super.fileName’ to access the parent class’s field.
Can you chain multiple ‘super’ calls?
No, You cannot chain multiple ‘super’ calls. Each constructor in the hierarchy can only call its immediate parent class constructor once, and this call must be the first statement in the constructor.
Hero Vired is a leading LearnTech company dedicated to offering cutting-edge programs in collaboration with top-tier global institutions. As part of the esteemed Hero Group, we are committed to revolutionizing the skill development landscape in India. Our programs, delivered by industry experts, are designed to empower professionals and students with the skills they need to thrive in today’s competitive job market.