×

Advanced Coding & Software Engineering Program

Duration: 1 Year (12 Months)

Join our premium 1-year program to master cutting-edge technologies and become an industry-ready Software Engineer!

Course Coverage

  • Languages: C, C++, Java, JavaScript, Python
  • Web Technologies: HTML, CSS, Bootstrap 5, MERN Stack, Full Stack Development
  • Databases: MySQL, MongoDB
  • Data Science Libraries: Pandas, NumPy
  • Development Tools: Visual Studio Code, IntelliJ IDEA, PyCharm, Postman, Git, GitHub
  • Cloud Platforms: Vercel, MongoDB Atlas

Program Highlights

  • Live Classes: Interactive sessions with real-time doubt resolution
  • Hands-On Sessions: Practical coding exercises to build real-world skills
  • Industry Experts: Learn from professionals with years of experience
  • Live Project: Work on real-world projects to apply your skills
  • Get Certificate: Earn a professional certificate upon program completion

Course Fee: Only ₹1020 / month
Limited Period Offer!

Java super Keyword Tutorial with Real-Life Example



Last Updated on: 12th Nov 2025 16:49:12 PM

Welcome to this beginner-friendly tutorial on the super keyword in Java! The super keyword is used in inheritance to refer to the immediate parent class. It helps access parent class members (variables, methods, and constructors) when they are hidden or overridden in the child class.

 

This tutorial uses a real-world project-based example — a Hospital Management System — to show how super is used in real software development.

 

What is the super Keyword?

In Java, the super keyword is used to refer to the immediate parent class. It helps when a subclass wants to access something (like variables, methods, or constructors) from its parent class that it has overridden or hidden.

 

super is a reference variable that refers to the immediate parent class object. It is used to:

  1. Call parent class constructor

  2. Access parent class method

  3. Access parent class variable

 

Uses of super Keyword

    Use

    Syntax

    Purpose

1. Call parent constructor

super() or super(args)

Initialize parent class fields

2. Call parent method

super.methodName()

Execute parent’s version

3. Access parent variable

super.variableName

Get parent’s field value

 

1. Using super to Access Parent Class Variable

class Animal {
    String name = "Animal";
}

class Dog extends Animal {
    String name = "Dog";

    void display() {
        System.out.println("Child class name: " + name);
        System.out.println("Parent class name: " + super.name);
    }
}

public class Main {
    public static void main(String[] args) {
        Dog obj = new Dog();
        obj.display();
    }
}

 

Output:

Child class name: Dog
Parent class name: Animal

 

Explanation:

  • super.name refers to the parent class variable (Animal).

  • name refers to the child class variable (Dog).

 

2. Using super to Call Parent Class Method & Constructor

class Course {
    Course() {
        System.out.println("Course is being created...");
    }

    void displayDetails() {
        System.out.println("This is a general course.");
    }
}

class JavaCourse extends Course {
    JavaCourse() {
        super(); // Calls Course constructor
        System.out.println("Java Course initialized successfully!");
    }

    void displayDetails() {
        super.displayDetails(); // Calls parent method
        System.out.println("This is a specialized Java Full Stack course.");
    }
}

public class Main {
    public static void main(String[] args) {
        JavaCourse jc = new JavaCourse();
        jc.displayDetails();
    }
}

 

Output:

Course is being created...
Java Course initialized successfully!
This is a general course.
This is a specialized Java Full Stack course.

 

Explanation:

  • When super() is used inside a constructor, it must be the first statement.

  • It calls the parent class constructor before executing the child class constructor.

 

Real-Time Project: Hospital Management System

Let’s build a Hospital App with:

  • Person Base class

  • Doctor, Nurse, Patient → Subclasses

 

We’ll use super to reuse and extend parent behavior.

 

Step 1: Parent Class – Person.java

// Person.java
public class Person {
    protected String name;
    protected int age;
    protected String contact;

    // Parent constructor
    public Person(String name, int age, String contact) {
        this.name = name;
        this.age = age;
        this.contact = contact;
        System.out.println("[Person] Created: " + name);
    }

    // Parent method
    public void displayBasicInfo() {
        System.out.println("Name: " + name);
        System.out.println("Age: " + age);
        System.out.println("Contact: " + contact);
    }

    // Method to be overridden
    public void showRole() {
        System.out.println("Role: General Person");
    }
}

 

Step 2: Child Classes Using super

1. Doctor.java

public class Doctor extends Person {
    private String specialization;
    private String licenseNo;
    private double salary;

    // Use super() to call parent constructor
    public Doctor(String name, int age, String contact, 
                  String specialization, String licenseNo, double salary) {
        super(name, age, contact);  // Call parent constructor
        this.specialization = specialization;
        this.licenseNo = licenseNo;
        this.salary = salary;
        System.out.println("[Doctor] Assigned: " + specialization);
    }

    // Override parent method
    @Override
    public void showRole() {
        System.out.println("Role: Doctor");
    }

    // Use super.method() to extend parent behavior
    public void displayFullInfo() {
        super.displayBasicInfo();  // Call parent method
        System.out.println("Specialization: " + specialization);
        System.out.println("License: " + licenseNo);
        System.out.println("Salary: ₹" + salary);
    }
}

 

2. Nurse.java

public class Nurse extends Person {
    private String department;
    private int shiftHours;

    public Nurse(String name, int age, String contact, 
                 String department, int shiftHours) {
        super(name, age, contact);
        this.department = department;
        this.shiftHours = shiftHours;
        System.out.println("[Nurse] Assigned to: " + department);
    }

    @Override
    public void showRole() {
        System.out.println("Role: Nurse");
    }

    public void displayShift() {
        super.displayBasicInfo();  // Reuse parent
        System.out.println("Department: " + department);
        System.out.println("Shift: " + shiftHours + " hours");
    }
}

 

3. Patient.java

public class Patient extends Person {
    private String patientId;
    private String ailment;
    private String admittedDate;

    public Patient(String name, int age, String contact, 
                   String patientId, String ailment, String admittedDate) {
        super(name, age, contact);
        this.patientId = patientId;
        this.ailment = ailment;
        this.admittedDate = admittedDate;
        System.out.println("[Patient] Admitted for: " + ailment);
    }

    @Override
    public void showRole() {
        System.out.println("Role: Patient");
    }

    public void displayRecord() {
        super.displayBasicInfo();
        System.out.println("Patient ID: " + patientId);
        System.out.println("Ailment: " + ailment);
        System.out.println("Admitted: " + admittedDate);
    }
}

 

Step 3: Main App – HospitalManagement.java

// HospitalManagement.java
import java.util.ArrayList;
import java.util.List;

public class HospitalManagement {
    public static void main(String[] args) {
        System.out.println("HOSPITAL MANAGEMENT SYSTEM\n");

        // Create objects
        Doctor doc = new Doctor("Dr. Rajesh Kumar", 45, "9876543210",
                                "Cardiologist", "LIC-789", 250000);

        Nurse nurse = new Nurse("Anita Sharma", 32, "9123456789",
                                "ICU", 8);

        Patient patient = new Patient("Vikram Singh", 28, "9988776655",
                                      "P-1001", "Fever & Cough", "2025-04-05");

        System.out.println("\n" + "=".repeat(50));

        // Use overridden + super methods
        List<Person> staff = new ArrayList<>();
        staff.add(doc);
        staff.add(nurse);
        staff.add(patient);

        for (Person p : staff) {
            p.showRole();           // Runtime polymorphism
            if (p instanceof Doctor) {
                ((Doctor) p).displayFullInfo();
            } else if (p instanceof Nurse) {
                ((Nurse) p).displayShift();
            } else if (p instanceof Patient) {
                ((Patient) p).displayRecord();
            }
            System.out.println("-".repeat(30));
        }
    }
}

 

Output

HOSPITAL MANAGEMENT SYSTEM

[Person] Created: Dr. Rajesh Kumar
[Doctor] Assigned: Cardiologist
[Person] Created: Anita Sharma
[Nurse] Assigned to: ICU
[Person] Created: Vikram Singh
[Patient] Admitted for: Fever & Cough

==================================================
Role: Doctor
Name: Dr. Rajesh Kumar
Age: 45
Contact: 9876543210
Specialization: Cardiologist
License: LIC-789
Salary: ₹250000.0
------------------------------
Role: Nurse
Name: Anita Sharma
Age: 32
Contact: 9123456789
Department: ICU
Shift: 8 hours
------------------------------
Role: Patient
Name: Vikram Singh
Age: 28
Contact: 9988776655
Patient ID: P-1001
Ailment: Fever & Cough
Admitted: 2025-04-05
------------------------------

 

When to Use super?

    Scenario

    Use super

 Parent and child have same variable name

 super.name

 Child wants to extend parent method

 super.method()

 Must initialize parent fields

 super(args) in constructor

 

Rules & Best Practices

  1. super() must be the first statement in constructor

  2. Use @Override with super.method() for clarity

  3. super cannot be used in static context

  4. Always call super() if parent has no default constructor

// Valid
public Child() {
    super("default");  // First line
    // other code
}

// Invalid
public Child() {
    // some code
    super();  // COMPILE ERROR!
}

 

Real-Time Use Cases

   System

   super Usage

 E-commerce

 Product Electronics Mobile (call super.display())

 HR System

 Employee Manager (reuse calculateSalary())

 Game Engine

 GameObject Player, Enemy (call super.update())

 

Common Mistakes 

// Wrong: Missing super() when parent has parameterized constructor
class Parent {
    Parent(String x) { }
}
class Child extends Parent {  // ERROR if no super()
}

// Correct
class Child extends Parent {
    Child() {
        super("default");
    }
}

 

Summary

  • super = Reference to parent class

  • 3 Uses:

    1. super() → Call parent constructor

    2. super.method() → Call parent method

    3. super.var → Access parent variable

  • Essential for code reuse and initialization

  • Used in every real-world inheritance project

 

Key Points to Remember

✅ super refers to the immediate parent class only (not the grandparent).
✅ You can’t use super inside static methods.
✅ super() must be the first line in a constructor if used.
✅ If you don’t explicitly call super(), Java automatically calls the no-argument constructor of the parent class.

 

Project Tip:
In your Hospital App, use super in:

  • Staff Doctor, Nurse

  • Billable Consultation, Surgery

  • User Admin, Receptionist

 


You now fully understand the super keyword with a real-world hospital system!
Keep practicing — happy coding!  yes


Online - Chat Now
Let’s Connect

Inquiry Sent!

Your message has been successfully sent. We'll get back to you soon!

iKeySkills Logo