Skip to content
C

super Keyword

super refers to the immediate parent class. It's used to access the parent class's fields, methods, or constructor from within a subclass.


1. What is the super Keyword?

super refers to the immediate parent class. It's used to access the parent class's fields, methods, or constructor from within a subclass.

2. Why is it used?

Sometimes a subclass needs to specifically call the parent's version of a method (especially if it has overridden that method), or needs to pass values up to the parent's constructor. super provides this direct link to the parent.

3. Real-Life Example

Think of a child referring to "my parent's rules" specifically, when their own rules might be slightly different. super lets code specifically point to the parent class's version of something, even inside a subclass.

4. Syntax

java
super.fieldName; super.methodName(); super(parameters); // calls parent constructor

5. Example Program

java
class Animal { Animal() { System.out.println("Animal created"); } } class Dog extends Animal { Dog() { super(); // calls Animal's constructor System.out.println("Dog created"); } } public class SuperDemo { public static void main(String[] args) { new Dog(); } }

Output:

Animal created
Dog created

6. Key Points to Remember

  • super() must be the first statement inside a constructor, if used.
  • If you don't explicitly call super(), Java automatically calls the parent's no-argument constructor first.
  • super is also useful to access an overridden method's original version from the parent class.