DevAcademia
C++C#CPythonJava
  • Java Basics

  • Java Introduction
  • Java Get Started
  • Java Syntax
  • Java Output
  • Java Comments
  • Java Variables
  • Java Data Types
  • Java Type Casting
  • Java Operators
  • Java Strings
  • Java If...Else
  • Java Switch Statement
  • Java Loops
  • Java Math
  • Java Arrays
  • Java Date
  • Java OOP

  • Java Classes/Objects
  • Java Class Attributes
  • Java Class Methods
  • Java Constructors
  • Java Destructors
  • Java this Keyword
  • Java Modifiers
  • Java Non Modifiers
  • Java Encapsulation
  • Java Packages & API
  • Java Inheritance
  • Java Polymorphism
  • Java Super Keyword
  • Java Inner Classes
  • Java Exception Handling
  • Java Abstraction
  • Java Interfaces
  • Java Enums
  • Java User Input
  • Java Quiz

  • Java Fundamentals Quiz
  • Java Basics

  • Java Introduction
  • Java Get Started
  • Java Syntax
  • Java Output
  • Java Comments
  • Java Variables
  • Java Data Types
  • Java Type Casting
  • Java Operators
  • Java Strings
  • Java If...Else
  • Java Switch Statement
  • Java Loops
  • Java Math
  • Java Arrays
  • Java Date
  • Java OOP

  • Java Classes/Objects
  • Java Class Attributes
  • Java Class Methods
  • Java Constructors
  • Java Destructors
  • Java this Keyword
  • Java Modifiers
  • Java Non Modifiers
  • Java Encapsulation
  • Java Packages & API
  • Java Inheritance
  • Java Polymorphism
  • Java Super Keyword
  • Java Inner Classes
  • Java Exception Handling
  • Java Abstraction
  • Java Interfaces
  • Java Enums
  • Java User Input
  • Java Quiz

  • Java Fundamentals Quiz

Loading Java tutorial…

Loading content
Java OOPTopic 52 of 59
←PreviousPrevNextNext→

Java super Keyword

Introduction to super Keyword

The super keyword in Java is used to refer to the immediate parent class. It is mainly used for:

1. Accessing parent class fields and methods hidden by subclass members with the same name

2. Calling parent class constructors from subclass constructors

3. Differentiating between superclass and subclass members when they share the same identifier

Uses of super Keyword

UsageSyntaxDescription
Access superclass variablesuper.variableNameAccesses the hidden variable from the superclass
Call superclass methodsuper.methodName()Invokes the overridden method from the superclass
Call superclass constructorsuper() or super(params)Must be the first statement in a subclass constructor

Accessing Superclass Members

If a subclass defines members with the same name as those in its superclass, the super keyword allows direct access to the superclass version of those members.

Example
class Animal {
    protected String name = "Animal";
    
    public void makeSound() {
        System.out.println("Animal makes a sound");
    }
}

class Dog extends Animal {
    private String name = "Dog"; // Hides superclass variable
    
    public void displayNames() {
        System.out.println("Subclass name: " + name);
        System.out.println("Superclass name: " + super.name); // Access superclass variable
    }
    
    @Override
    public void makeSound() {
        System.out.println("Dog barks");
    }
    
    public void makeAnimalSound() {
        super.makeSound(); // Call superclass method
    }
}

public class SuperExample {
    public static void main(String[] args) {
        Dog dog = new Dog();
        dog.displayNames();
        dog.makeSound();
        dog.makeAnimalSound();
    }
}
Output
Subclass name: Dog
Superclass name: Animal
Dog barks
Animal makes a sound

Using super() to Call Superclass Constructors

The call to super() must be the first statement in a subclass constructor. If it is omitted, the compiler automatically inserts a call to the no-argument constructor of the superclass.

Example
class Person {
    protected String name;
    protected int age;
    
    public Person() {
        System.out.println("Person no-arg constructor called");
    }
    
    public Person(String name, int age) {
        this.name = name;
        this.age = age;
        System.out.println("Person parameterized constructor called");
    }
}

class Student extends Person {
    private int studentId;
    
    public Student() {
        // super() is implicitly called here
        System.out.println("Student no-arg constructor called");
    }
    
    public Student(String name, int age, int studentId) {
        super(name, age); // Must be the first statement
        this.studentId = studentId;
        System.out.println("Student parameterized constructor called");
    }
    
    public void display() {
        System.out.println("Name: " + name + ", Age: " + age + ", ID: " + studentId);
    }
}

public class SuperConstructorExample {
    public static void main(String[] args) {
        System.out.println("Creating student with no args:");
        Student student1 = new Student();
        
        System.out.println("\nCreating student with parameters:");
        Student student2 = new Student("Alice", 20, 12345);
        student2.display();
    }
}
Output
Creating student with no args:
Person no-arg constructor called
Student no-arg constructor called

Creating student with parameters:
Person parameterized constructor called
Student parameterized constructor called
Name: Alice, Age: 20, ID: 12345

super in Multilevel Inheritance

In multilevel inheritance, the super keyword always refers to the immediate parent class, not ancestors further up the hierarchy.

Example
class Grandparent {
    protected String message = "Grandparent message";
}

class Parent extends Grandparent {
    protected String message = "Parent message";
}

class Child extends Parent {
    private String message = "Child message";
    
    public void displayMessages() {
        System.out.println("Child: " + message);
        System.out.println("Parent: " + super.message); // Immediate parent
        // To access the grandparent's message, explicit casting or helper methods would be needed
    }
}

public class MultilevelSuper {
    public static void main(String[] args) {
        Child child = new Child();
        child.displayMessages();
    }
}
Output
Child: Child message
Parent: Parent message

Best Practices

- Use super to resolve naming conflicts between subclass and superclass members

- Always call the appropriate superclass constructor using super() when subclass constructors require it

- Remember that super() must appear as the first statement in a constructor

- Use super.method() to extend superclass functionality without fully replacing it

- Avoid overly deep inheritance hierarchies, as managing super calls can become complex

Test your knowledge: Java super Keyword
Quiz Configuration
4 of 8 questions
Sequential
Previous allowed
Review enabled
Early close allowed
Estimated time: 5 min
Java OOPTopic 52 of 59
←PreviousPrevNextNext→