Object-Oriented Programming · Lesson 54 of 150
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.
01What 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.
02Why 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.
03Real-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.
04Syntax
javasuper.fieldName; super.methodName(); super(parameters); // calls parent constructor
05Example Program
javaclass 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