Super Keyword in Java with Examples
Get Job-ready: Java Course with 45+ Real-time Projects! - Learn Java
In Java programming, the unique word “super” is a key to understanding how classes work together. It’s not just an ordinary word; it’s a powerful tool connecting parent and child classes in object-oriented programming.
This article is here to help you understand the special feature called “super” in Java. We will discuss what it’s used for, how it’s connected to inheritance, and how it allows polymorphism to occur.
Whether you’re new to coding or an experienced Java developer, join us to discover how “super” can make your Java programs more functional and flexible. This journey demystifies this important Java concept, helping you write better code.
Characteristics of Super Keyword in Java
The super keyword has the following notable characteristics:
Used to Invoke Superclass Constructor: super() calls the superclass’s constructor from within a subclass constructor. This is important because the superclass constructor is responsible for initializing the state of the superclass. Using super() ensures the superclass is properly initialized before the subclass.
Used to Access Superclass Members: Super can be used to access superclass fields that may be hidden by subclass fields with the same name. It can also invoke superclass methods even when the subclass overrides them with its own implementation, giving flexibility to retain superclass behavior.
Must be First Statement in Constructor: Within a subclass constructor, super() must be the first statement executed. This enforces proper initialization of the inheritance chain from superclass to subclass.
Cannot Use super in Static Context: The super keyword pertains to an object instance and its connections within the inheritance hierarchy. Therefore, super cannot be used in static contexts, such as a static variable or static method, which do not operate on specific object instances.
Not Required to Invoke Superclass Methods: Using super to call superclass methods is optional. Methods can be called directly on the subclass instance. However, super may need to be disambiguated between superclass and subclass methods.
Use of super Keyword in Java
1) Use of super with Variables
We can use super to access superclass fields hidden by a subclass field with the same name. For example:
public class Shape {
int color = 0;
}
public class Rectangle extends Shape {
int color = 1;
int getSuperColor() {
return super.color; //returns 0
}
}2) Use of super with Methods
The super keyword is used to call superclass methods when subclass has a method with same name. For example:
public class Shape {
public void printDetails() {
System.out.println("This is a shape.");
}
}
public class Rectangle extends Shape {
public void printDetails() {
super.printDetails();
System.out.println("This is a rectangle.");
}
}Here super.printDetails() ensures that the behavior of the superclass method is retained.
3) Use of super with Constructors
super() invokes the superclass constructor.
For example:
public class Shape {
public Shape() {
System.out.println("Shape constructor called");
}
}
public class Rectangle extends Shape {
public Rectangle() {
super();
System.out.println("Rectangle constructor called");
}
}This calls the Shape constructor before executing the Rectangle constructor.
Advantages of Using Java super keyword
Some key advantages of using the super keyword in Java:
- Enables code reuse through inheritance.
- Supports polymorphism by allowing methods to be overridden.
- Provides access to superclass members when needed.
- Allows customization of superclass behavior.
- Facilitates abstraction by separating behaviors into classes.
- Encapsulates implementation details in the superclass.
Important Points to Remember
Some important things to remember about super():
1) The Importance of super() Being the First Statement in the Derived(Student) Class Constructor: The rule that super() should be the initial action in the Derived (Student) Class constructor arises from the fact that the superclass, by design, lacks any knowledge of its subclasses. Consequently, any setup or preparation it requires must occur independently and, in some cases, ahead of any actions the subclass performs. As such, the superclass must complete its setup before anything else.
2) Automatic Insertion of Superclass Constructor Call When Not Explicitly Invoked: In Java, if a constructor doesn’t mention the constructor of its parent class, the Java compiler will automatically add a call to the parent class’s constructor that doesn’t need any arguments. But if the parent class doesn’t have such a constructor, you’ll get an error when you try to compile your code. This usually isn’t a problem when the parent class is the Object class because it always has a constructor that doesn’t need any arguments.
3) Understanding Constructor Chaining in Subclasses: When a subclass constructor invokes a constructor from its superclass, explicitly or implicitly, it may seem like a cascading sequence of constructors is triggered, potentially reaching the Object class constructor. This phenomenon is indeed known as “constructor chaining.”
Conclusion
In simple terms, the super keyword is like a bridge that connects different parts of a Java program, making them work together. It helps junior parts of the code (subclasses) borrow and change things from senior parts (superclasses). This borrowing and changing helps write less code and make programs more flexible.
Where you put “super()” in your code is like saying, “Start here!” It’s like telling a chef to start with a special recipe. And this special recipe makes sure everything runs smoothly.
So, in a nutshell, the super keyword makes your Java code neat and tidy, easier to use and understand, and more efficient. It’s like having a super helper that keeps your code in order and follows the important rules of Java programming.
Did you like our efforts? If Yes, please give DataFlair 5 Stars on Google


How I can learn super keyword as a expert in java
Practice over on it