Java super Keyword
Accessing parent class members and constructors
⬆️ What is the super Keyword?
The super keyword refers to the immediate parent class. It's used to access parent class methods, variables, and constructors from the child class.
class Parent {
String name = "Parent";
}
class Child extends Parent {
String name = "Child";
public void showNames() {
System.out.println("Child name: " + name);
System.out.println("Parent name: " + super.name);
}
}
Output:
Child name: Child
Parent name: Parent
Uses of super Keyword
Constructor Call
Call parent class constructor
super(name, age);
Access Variables
Access parent class variables
super.parentVariable
Call Methods
Call parent class methods
super.parentMethod();
Override Extension
Extend overridden methods
super.method();
// Add more functionality
🔹 super() Constructor Call
Use super() to call the parent class constructor:
class Vehicle {
protected String brand;
protected int year;
public Vehicle(String brand, int year) {
this.brand = brand;
this.year = year;
System.out.println("Vehicle constructor called");
}
public void start() {
System.out.println(brand + " is starting");
}
}
class Car extends Vehicle {
private int doors;
public Car(String brand, int year, int doors) {
super(brand, year); // Must be first line
this.doors = doors;
System.out.println("Car constructor called");
}
public void showInfo() {
System.out.println(brand + " " + year + " with " + doors + " doors");
}
}
// Usage
public class Main {
public static void main(String[] args) {
Car car = new Car("Toyota", 2023, 4);
car.showInfo();
car.start();
}
}
Output:
Vehicle constructor called
Car constructor called
Toyota 2023 with 4 doors
Toyota is starting
🔹 Accessing Parent Variables
Use super to access parent class variables when child has same name:
class Animal {
String type = "Generic Animal";
public void showType() {
System.out.println("I am a " + type);
}
}
class Dog extends Animal {
String type = "Dog"; // Hides parent variable
public void showBothTypes() {
System.out.println("Child type: " + type);
System.out.println("Parent type: " + super.type);
}
public void callBothMethods() {
showType(); // Calls inherited method
super.showType(); // Explicitly calls parent method
}
}
// Usage
Dog dog = new Dog();
dog.showBothTypes();
dog.callBothMethods();
Output:
Child type: Dog
Parent type: Generic Animal
I am a Dog
I am a Generic Animal
🔹 Extending Overridden Methods
Use super to extend parent functionality instead of completely replacing it:
class Employee {
protected String name;
protected double salary;
public Employee(String name, double salary) {
this.name = name;
this.salary = salary;
}
public void work() {
System.out.println(name + " is working");
}
public double calculatePay() {
return salary;
}
}
class Manager extends Employee {
private double bonus;
public Manager(String name, double salary, double bonus) {
super(name, salary);
this.bonus = bonus;
}
@Override
public void work() {
super.work(); // Call parent method first
System.out.println(name + " is also managing the team");
}
@Override
public double calculatePay() {
double basePay = super.calculatePay(); // Get parent calculation
return basePay + bonus; // Add manager-specific bonus
}
}
// Usage
Manager manager = new Manager("Alice", 5000, 1000);
manager.work();
System.out.println("Total pay: $" + manager.calculatePay());
Output:
Alice is working
Alice is also managing the team
Total pay: $6000.0
🔹 Important super Rules
- Constructor call: super() must be the first statement in child constructor
- Automatic call: If you don't call super(), Java automatically calls super()
- No chaining: Cannot use super.super to access grandparent
- Static context: Cannot use super in static methods
class Parent {
public Parent() {
System.out.println("Parent constructor");
}
}
class Child extends Parent {
public Child() {
// super(); is automatically called here
System.out.println("Child constructor");
}
public Child(String name) {
super(); // Explicit call (optional in this case)
System.out.println("Child constructor with name: " + name);
}
}