π Introduction
In the previous articles, we explored:
- Inheritance
- Types of Inheritance
- Method Overriding
- Parent Reference & Child Object
Now comes a very important concept that connects all of these together:
π The super keyword
When we use inheritance, a child class inherits properties and methods from the parent class. But sometimes, the child class:
- Has variables with the same name as the parent
- Overrides parent methods
- Needs to call the parent constructor
In all these cases, Java provides a special keyword:
π super
Understanding super is essential because it helps us control and access parent class behavior, especially when overriding happens.
π§ What is super Keyword?
π super is a reference variable in Java that is used to refer to the immediate parent class object.
In simple words:
superallows a child class to access the parent class members.
π― Where is super Used?
The super keyword is mainly used in three ways:
1οΈβ£ To access parent class variables
2οΈβ£ To call parent class methods
3οΈβ£ To call parent class constructors
Letβs understand each one in detail.
πΉ 1. Accessing Parent Class Variable Using super
π» Example
class Animal {
String name = "Animal";
}
class Dog extends Animal {
String name = "Dog";
void printName() {
System.out.println(name);
System.out.println(super.name);
}
}
public class Main {
public static void main(String[] args) {
Dog d = new Dog();
d.printName();
}
}
π Explanation
String name = "Animal";
Parent class variable
String name = "Dog";
Child class variable (same name)
System.out.println(name);
π Refers to current class variable (Dog)
System.out.println(super.name);
π Refers to parent class variable (Animal)
π§Ύ Output
Dog
Animal
β οΈ Important Concept
π If both parent and child have same variable names:
this.nameβ child classsuper.nameβ parent class
πΉ 2. Calling Parent Class Method Using super
π» Example
class Animal {
void sound() {
System.out.println("Animal makes sound");
}
}
class Dog extends Animal {
void sound() {
super.sound();
System.out.println("Dog barks");
}
}
public class Main {
public static void main(String[] args) {
Dog d = new Dog();
d.sound();
}
}
π Explanation
Child method:
void sound()
Overrides parent method
super.sound();
π Calls parent class version of method
π§Ύ Output
Animal makes sound
Dog barks
π₯ Key Insight
π Without super, parent method would not be called
π With super, both parent + child behavior can be combined
πΉ 3. Calling Parent Class Constructor Using super()
π» Example
class Animal {
Animal() {
System.out.println("Animal Constructor");
}
}
class Dog extends Animal {
Dog() {
super();
System.out.println("Dog Constructor");
}
}
public class Main {
public static void main(String[] args) {
Dog d = new Dog();
}
}
π Explanation
super();
π Calls parent class constructor
π§Ύ Output
Animal Constructor
Dog Constructor
β οΈ Important Rule
π super() must be the first statement in constructor
π§ Hidden Behavior
Even if you donβt write:
super();
π Java automatically inserts it (default constructor case)
βοΈ Rules of super Keyword
β Rule 1: Refers to Immediate Parent
super can only access direct parent, not grandparent.
β Rule 2: Cannot Be Used in Static Context
static void method() {
// super not allowed here β
}
β Rule 3: Must Be First Statement in Constructor
Dog() {
super(); // must be first
}
β Rule 4: Used Only Inside Child Class
super is meaningful only in inheritance
π this vs super (Very Important)
| Feature | this | super |
|---|---|---|
| Refers to | Current class | Parent class |
| Access | Own members | Parent members |
| Constructor | this() | super() |
𧨠Tricky Interview Example
class A {
int x = 10;
}
class B extends A {
int x = 20;
void show() {
System.out.println(x);
System.out.println(super.x);
}
}
public class Main {
public static void main(String[] args) {
B obj = new B();
obj.show();
}
}
π§Ύ Output
20
10
β οΈ Common Mistakes & Confusions
β Mistake 1: Forgetting super() position
Dog() {
System.out.println("Dog");
super(); // β Error
}
β Mistake 2: Confusing this and super
π this β current class
π super β parent class
β Mistake 3: Using in static method
static void test() {
super.sound(); // β Not allowed
}
β Mistake 4: Thinking super accesses all ancestors
β Only immediate parent
π― Real-Life Analogy
Think:
Child β refers to Parent using "super"
Just like:
π You calling your parent for help
π§ Key Points to Remember
β super refers to parent class object
β Used for variables, methods, constructors
β Helps resolve overriding conflicts
β Must be first in constructor
β Cannot be used in static context
π Conclusion
The super keyword is a powerful feature in Java that allows a child class to access and interact with its parent class.
It plays a crucial role in:
- Method overriding
- Constructor chaining
- Resolving variable conflicts
In this article, we learned:
- What
superis - How to access parent variables
- How to call parent methods
- How constructors work with
super() - Common mistakes and interview traps
Mastering super helps you understand how inheritance works internally, making your code more structured and maintainable.