-
Hajipur, Bihar, 844101
The super keyword is a reference that points to the immediate parent class object. You use super when a subclass needs to access members (variables, methods, constructors) that belong to its parent. It resolves ambiguity when a child class defines members with the same name as the parent, and it lets the child call parent behavior explicitly.
In short, super connects a subclass to its superclass so the subclass can reuse or extend parent functionality without confusion.
super exists to make inheritance clearer and safer. Common reasons to use it:
Access a parent variable that is hidden by a child variable with the same name.
Invoke a parent class method that the child has overridden.
Call a specific parent constructor to ensure proper initialization.
Using super keeps inheritance behavior explicit and avoids accidental use of the wrong member when names collide. It also enforces a clear initialization order when constructors need to chain through the class hierarchy.
super refers to the immediate parent class only, not any ancestor beyond the direct parent.
You cannot use super in static methods because static context does not have an instance.
Calls to super() — the parent constructor — must be the first statement inside a child constructor.
If you do not explicitly call super() in a child constructor, the compiler inserts a no-argument super() automatically, provided the parent has a no-arg constructor.
You can use super to access hidden variables (super.varName), to call overridden methods (super.methodName(...)), and to invoke the parent constructor (super(...)).
class Parent {
String name = "Parent";
}
class Child extends Parent {
String name = "Child";
void showNames() {
System.out.println(name); // Child name
System.out.println(super.name); // Parent name
}
}
class Parent {
void speak() {
System.out.println("Parent speaks");
}
}
class Child extends Parent {
void speak() {
System.out.println("Child speaks");
}
void showSpeak() {
super.speak(); // calls Parent.speak()
speak(); // calls Child.speak()
}
}
class Parent {
Parent(String id) {
System.out.println("Parent: " + id);
}
}
class Child extends Parent {
Child(String id) {
super(id); // must be first statement
System.out.println("Child: " + id);
}
}
Think of a toolkit shared across a family business. The parent toolbox contains basic tools everyone can use. A new family member brings a personalized toolbox but still needs to access the original tools. Using super is like reaching into the family toolbox for a standard wrench named the same as the one you have locally; it makes clear you mean the family tool, not your personal one.
class Device {
String model = "Generic";
Device() {
System.out.println("Device constructed");
}
void status() {
System.out.println("Device model: " + model);
}
}
class Phone extends Device {
String model = "SmartPhone";
Phone() {
super(); // calls Device()
System.out.println("Phone constructed");
}
void status() {
System.out.println("Phone model: " + model);
System.out.println("Base model: " + super.model); // access parent variable
super.status(); // call parent method
}
}
public class Main {
public static void main(String[] args) {
Phone p = new Phone();
p.status();
}
}
Explanation: The Phone constructor explicitly calls super() so the Device constructor runs first. status() prints the child’s model, then uses super.model to print the parent model, then calls super.status() to run the parent method.
Placing super() anywhere other than the first statement in a constructor. Compiler error will occur.
Trying to use super inside static context (static methods or static blocks). It’s not allowed because super refers to an instance.
Confusing super with this. this refers to the current object; super refers to the parent part of the current object.
Assuming super can access private members of parent. It cannot. Private members remain inaccessible; super works with protected, package-private (if same package), and public members.
| Feature | this | super |
|---|---|---|
| Refers to | Current class object | Immediate parent class part of current object |
| Used for | Accessing this class variables/methods/constructors | Accessing parent class variables/methods/constructors |
| Constructor call | Calls other constructor in same class using this(...) | Calls parent constructor using super(...) (must be first) |
| Valid in static | No | No |
Clarifies code intent when names collide between parent and child.
Ensures proper initialization order across class hierarchy.
Allows reusing parent logic without copying code.
Makes overriding explicit while still allowing parent behavior to be invoked.
Use super.var to access parent variables hidden by child variables.
Use super.method(...) to call an overridden parent method.
Use super(...) to call a parent constructor; it must be the first statement in the constructor.
super works only in instance context and only with immediate parent.
Private parent members are not accessible via super.
The super keyword is a small but essential tool for working with inheritance in Java. It provides explicit access to the parent class’s constructors, methods, and variables and resolves ambiguity when names overlap. Use super to make inheritance relationships clear, to ensure correct initialization order, and to reuse parent behavior safely. Mastering super helps keep class hierarchies predictable and maintainable.
Create a class Person with a variable name and a subclass Student with a variable of the same name. Use super to display both the parent and child variable values.
Write a Java program where class Animal has a method sound(), and class Dog overrides it. Use super.sound() inside the child method to call the parent version as well.
Create a parent class Vehicle with a constructor that prints “Vehicle created”. Create a child class Car that calls the parent constructor using super() and prints “Car created”.
Write a Java program with classes Parent and Child. Both have a method showMessage(). In the child class, call both the parent and child methods using super.showMessage() and showMessage().
Create a parent class Bank with a variable rate = 7.0. Create a subclass SBI with a variable of the same name. Use super.rate to access the parent class rate.
Define a class Employee with a constructor that accepts id and name. Create a subclass Manager that passes these parameters to the parent constructor using super(id, name) and prints additional details.
Build a class hierarchy where Device is the parent and Mobile is the child. Override a method named displayInfo() and call the parent method using super.displayInfo().
Write a Java program with a parent class Shape having a method area(). Create subclass Rectangle that overrides the method but calls super.area() to print the base class message first.
Create a class A with a variable x = 100. Create a class B extending A with another variable x = 200. Use super.x to print both values in the child class.
Develop a program where class Parent has a parameterized constructor that initializes a message. The subclass Child calls the parent constructor using super("Message from child") and displays it.