Super Keyword in Constructors in Java

Introduction

Imagine you're building a family tree where each generation inherits traits from their parents, but also adds their own unique characteristics. The super keyword in Java constructors works exactly like thatโ€”it allows a child class to call its parent's constructor, ensuring the family lineage is properly established before adding new features!

The super keyword is like having a family handshake that passes down essential traits from parent to child before the child develops their own unique identity. It ensures that inheritance works correctly by properly initializing the parent part of an object before the child part.


What is the Super Keyword in Constructors?

The super keyword in constructors is used to call a parent class constructor from a child class constructor. It must be the first statement in the child constructor and ensures that the parent class is properly initialized before the child class.

Key Characteristics:

  • โœ… First statement: Must be the first line in child constructor
  • โœ… Constructor chaining: Calls parent class constructor
  • โœ… Explicit or implicit: Can be explicitly called or Java adds it automatically
  • โœ… Parameter passing: Can pass arguments to parent constructor
  • โœ… Inheritance enforcement: Ensures proper initialization hierarchy

Constructor Calling Hierarchy

Child Constructor
โ†“
super() โ†’ Parent Constructor
โ†“
super() โ†’ Grandparent Constructor
โ†“
... โ†’ Object Constructor

Code Explanation with Examples

Example 1: Basic Super() Usage

public class BasicSuperUsage {
public static void main(String[] args) {
System.out.println("=== BASIC SUPER() USAGE ===");
System.out.println("Creating Child object:");
Child child = new Child();
System.out.println("\nCreating ChildWithParams object:");
ChildWithParams childWithParams = new ChildWithParams("Hello from Child");
System.out.println("\nCreating GrandChild object:");
GrandChild grandChild = new GrandChild();
}
}
class Parent {
protected String parentData;
// Parent default constructor
public Parent() {
this.parentData = "Parent Default Data";
System.out.println("๐Ÿ‘จ Parent() constructor called");
System.out.println("   parentData: " + parentData);
}
// Parent parameterized constructor
public Parent(String data) {
this.parentData = data;
System.out.println("๐Ÿ‘จ Parent(String) constructor called");
System.out.println("   parentData: " + parentData);
}
}
class Child extends Parent {
private String childData;
// Child constructor - implicitly calls super()
public Child() {
// super() is called automatically by Java here
this.childData = "Child Data";
System.out.println("๐Ÿ‘ถ Child() constructor called");
System.out.println("   childData: " + childData);
System.out.println("   Inherited parentData: " + parentData);
}
}
class ChildWithParams extends Parent {
private String childData;
// Child constructor with explicit super() call
public ChildWithParams(String childData) {
super("Parent Data from Child"); // Explicit super call with parameter
this.childData = childData;
System.out.println("๐Ÿ‘ถ ChildWithParams(String) constructor called");
System.out.println("   childData: " + childData);
System.out.println("   Inherited parentData: " + parentData);
}
}
class GrandChild extends Child {
private String grandChildData;
public GrandChild() {
// Implicitly calls Child() constructor, which calls Parent() constructor
this.grandChildData = "GrandChild Data";
System.out.println("๐Ÿ‘ถ GrandChild() constructor called");
System.out.println("   grandChildData: " + grandChildData);
// Can access protected parentData from Parent class
System.out.println("   Inherited parentData: " + parentData);
}
}

Output:

=== BASIC SUPER() USAGE ===
Creating Child object:
๐Ÿ‘จ Parent() constructor called
parentData: Parent Default Data
๐Ÿ‘ถ Child() constructor called
childData: Child Data
Inherited parentData: Parent Default Data
Creating ChildWithParams object:
๐Ÿ‘จ Parent(String) constructor called
parentData: Parent Data from Child
๐Ÿ‘ถ ChildWithParams(String) constructor called
childData: Hello from Child
Inherited parentData: Parent Data from Child
Creating GrandChild object:
๐Ÿ‘จ Parent() constructor called
parentData: Parent Default Data
๐Ÿ‘ถ Child() constructor called
childData: Child Data
Inherited parentData: Parent Default Data
๐Ÿ‘ถ GrandChild() constructor called
grandChildData: GrandChild Data
Inherited parentData: Parent Default Data

Example 2: Super with Parameters and Constructor Overloading

public class SuperWithParameters {
public static void main(String[] args) {
System.out.println("=== SUPER WITH PARAMETERS ===");
System.out.println("Creating Car:");
Car car = new Car("Toyota", "Camry", 2023);
System.out.println("\nCreating ElectricCar:");
ElectricCar electricCar = new ElectricCar("Tesla", "Model S", 2024, 100);
System.out.println("\nCreating SportsCar:");
SportsCar sportsCar = new SportsCar("Ferrari", "488", 2024, 210);
}
}
class Vehicle {
protected String make;
protected String model;
protected int year;
// Default constructor
public Vehicle() {
this.make = "Unknown";
this.model = "Unknown";
this.year = 2024;
System.out.println("๐Ÿš— Vehicle() constructor");
}
// Parameterized constructor
public Vehicle(String make, String model, int year) {
this.make = make;
this.model = model;
this.year = year;
System.out.println("๐Ÿš— Vehicle(String, String, int) constructor");
System.out.println("   Make: " + make + ", Model: " + model + ", Year: " + year);
}
public void displayInfo() {
System.out.println("Vehicle: " + year + " " + make + " " + model);
}
}
class Car extends Vehicle {
protected int doors;
// Car constructor calling parent parameterized constructor
public Car(String make, String model, int year) {
super(make, model, year); // Call parent constructor with parameters
this.doors = 4; // Default number of doors
System.out.println("๐Ÿš™ Car(String, String, int) constructor");
System.out.println("   Doors: " + doors);
}
// Another constructor calling parent default constructor
public Car() {
super(); // Explicit call to parent default constructor
this.doors = 4;
System.out.println("๐Ÿš™ Car() constructor");
}
}
class ElectricCar extends Car {
private int batteryCapacity;
public ElectricCar(String make, String model, int year, int batteryCapacity) {
super(make, model, year); // Call Car constructor, which calls Vehicle constructor
this.batteryCapacity = batteryCapacity;
System.out.println("โšก ElectricCar(String, String, int, int) constructor");
System.out.println("   Battery: " + batteryCapacity + " kWh");
}
}
class SportsCar extends Car {
private int topSpeed;
public SportsCar(String make, String model, int year, int topSpeed) {
super(make, model, year); // Call Car constructor
this.topSpeed = topSpeed;
System.out.println("๐ŸŽ๏ธ SportsCar(String, String, int, int) constructor");
System.out.println("   Top Speed: " + topSpeed + " mph");
}
// SportsCar with different parameter order
public SportsCar(int topSpeed, String make, String model) {
super(make, model, 2024); // Call Car constructor with specific year
this.topSpeed = topSpeed;
System.out.println("๐ŸŽ๏ธ SportsCar(int, String, String) constructor");
}
}

Output:

=== SUPER WITH PARAMETERS ===
Creating Car:
๐Ÿš— Vehicle(String, String, int) constructor
Make: Toyota, Model: Camry, Year: 2023
๐Ÿš™ Car(String, String, int) constructor
Doors: 4
Creating ElectricCar:
๐Ÿš— Vehicle(String, String, int) constructor
Make: Tesla, Model: Model S, Year: 2024
๐Ÿš™ Car(String, String, int) constructor
Doors: 4
โšก ElectricCar(String, String, int, int) constructor
Battery: 100 kWh
Creating SportsCar:
๐Ÿš— Vehicle(String, String, int) constructor
Make: Ferrari, Model: 488, Year: 2024
๐Ÿš™ Car(String, String, int) constructor
Doors: 4
๐ŸŽ๏ธ SportsCar(String, String, int, int) constructor
Top Speed: 210 mph

Example 3: Real-World Inheritance Hierarchy

public class RealWorldInheritance {
public static void main(String[] args) {
System.out.println("=== REAL-WORLD INHERITANCE HIERARCHY ===");
System.out.println("Creating Employee:");
Employee employee = new Employee("John Doe", "E1001", 50000);
System.out.println("\nCreating Manager:");
Manager manager = new Manager("Alice Smith", "M2001", 80000, "Engineering");
System.out.println("\nCreating Director:");
Director director = new Director("Bob Johnson", "D3001", 120000, "Technology", 500000);
System.out.println("\n=== DISPLAYING INFORMATION ===");
employee.displayInfo();
System.out.println();
manager.displayInfo();
System.out.println();
director.displayInfo();
}
}
class Person {
protected String name;
protected String id;
public Person(String name, String id) {
this.name = name;
this.id = id;
System.out.println("๐Ÿ‘ค Person(String, String) constructor");
System.out.println("   Name: " + name + ", ID: " + id);
}
public void displayInfo() {
System.out.println("Person: " + name + " (" + id + ")");
}
}
class Employee extends Person {
protected double salary;
public Employee(String name, String id, double salary) {
super(name, id); // Call Person constructor
this.salary = salary;
System.out.println("๐Ÿ’ผ Employee(String, String, double) constructor");
System.out.println("   Salary: $" + salary);
}
@Override
public void displayInfo() {
super.displayInfo(); // Call parent method
System.out.println("   Salary: $" + salary);
}
public double getAnnualBonus() {
return salary * 0.1; // 10% bonus
}
}
class Manager extends Employee {
protected String department;
public Manager(String name, String id, double salary, String department) {
super(name, id, salary); // Call Employee constructor
this.department = department;
System.out.println("๐Ÿ‘” Manager(String, String, double, String) constructor");
System.out.println("   Department: " + department);
}
@Override
public void displayInfo() {
super.displayInfo(); // Call Employee displayInfo
System.out.println("   Department: " + department);
}
@Override
public double getAnnualBonus() {
return salary * 0.15; // Managers get 15% bonus
}
public void conductMeeting() {
System.out.println("Manager " + name + " is conducting a meeting in " + department);
}
}
class Director extends Manager {
private double budget;
public Director(String name, String id, double salary, String department, double budget) {
super(name, id, salary, department); // Call Manager constructor
this.budget = budget;
System.out.println("๐ŸŽฏ Director(String, String, double, String, double) constructor");
System.out.println("   Budget: $" + budget);
}
@Override
public void displayInfo() {
super.displayInfo(); // Call Manager displayInfo
System.out.println("   Budget: $" + budget);
}
@Override
public double getAnnualBonus() {
return salary * 0.20; // Directors get 20% bonus
}
public void approveBudget() {
System.out.println("Director " + name + " approved budget of $" + budget);
}
}

Output:

=== REAL-WORLD INHERITANCE HIERARCHY ===
Creating Employee:
๐Ÿ‘ค Person(String, String) constructor
Name: John Doe, ID: E1001
๐Ÿ’ผ Employee(String, String, double) constructor
Salary: $50000.0
Creating Manager:
๐Ÿ‘ค Person(String, String) constructor
Name: Alice Smith, ID: M2001
๐Ÿ’ผ Employee(String, String, double) constructor
Salary: $80000.0
๐Ÿ‘” Manager(String, String, double, String) constructor
Department: Engineering
Creating Director:
๐Ÿ‘ค Person(String, String) constructor
Name: Bob Johnson, ID: D3001
๐Ÿ’ผ Employee(String, String, double) constructor
Salary: $120000.0
๐Ÿ‘” Manager(String, String, double, String) constructor
Department: Technology
๐ŸŽฏ Director(String, String, double, String, double) constructor
Budget: $500000.0
=== DISPLAYING INFORMATION ===
Person: John Doe (E1001)
Salary: $50000.0
Person: Alice Smith (M2001)
Salary: $80000.0
Department: Engineering
Person: Bob Johnson (D3001)
Salary: $120000.0
Department: Technology
Budget: $500000.0

Example 4: Super() in Complex Scenarios

public class ComplexSuperScenarios {
public static void main(String[] args) {
System.out.println("=== COMPLEX SUPER() SCENARIOS ===");
System.out.println("Creating SmartDevice:");
SmartDevice device = new SmartDevice("Generic Device", "1.0");
System.out.println("\nCreating SmartPhone:");
SmartPhone phone = new SmartPhone("iPhone 15", "iOS 17", "A17 Pro", 8);
System.out.println("\nCreating GamingPhone:");
GamingPhone gamingPhone = new GamingPhone("ROG Phone 7", "Android 13", "Snapdragon 8 Gen 2", 16, "AirTrigger");
System.out.println("\n=== DEVICE CAPABILITIES ===");
device.displayInfo();
System.out.println();
phone.displayInfo();
System.out.println();
gamingPhone.displayInfo();
}
}
class Device {
protected String deviceName;
protected String serialNumber;
protected boolean poweredOn;
public Device(String deviceName) {
this.deviceName = deviceName;
this.serialNumber = generateSerialNumber();
this.poweredOn = false;
System.out.println("๐Ÿ“ฑ Device(String) constructor");
System.out.println("   Device: " + deviceName + ", Serial: " + serialNumber);
}
private String generateSerialNumber() {
return "SN" + System.currentTimeMillis();
}
public void powerOn() {
this.poweredOn = true;
System.out.println(deviceName + " is now powered ON");
}
public void powerOff() {
this.poweredOn = false;
System.out.println(deviceName + " is now powered OFF");
}
public void displayInfo() {
System.out.println("Device: " + deviceName + " (" + serialNumber + ")");
System.out.println("Powered: " + (poweredOn ? "ON" : "OFF"));
}
}
class SmartDevice extends Device {
protected String osVersion;
protected boolean connectedToInternet;
public SmartDevice(String deviceName, String osVersion) {
super(deviceName); // Call Device constructor
this.osVersion = osVersion;
this.connectedToInternet = false;
System.out.println("๐Ÿ“Ÿ SmartDevice(String, String) constructor");
System.out.println("   OS: " + osVersion);
}
public void connectToInternet() {
this.connectedToInternet = true;
System.out.println(deviceName + " connected to internet");
}
public void disconnectFromInternet() {
this.connectedToInternet = false;
System.out.println(deviceName + " disconnected from internet");
}
@Override
public void displayInfo() {
super.displayInfo(); // Call Device displayInfo
System.out.println("   OS Version: " + osVersion);
System.out.println("   Internet: " + (connectedToInternet ? "Connected" : "Disconnected"));
}
}
class SmartPhone extends SmartDevice {
protected String processor;
protected int ramGB;
protected boolean cellularConnected;
public SmartPhone(String deviceName, String osVersion, String processor, int ramGB) {
super(deviceName, osVersion); // Call SmartDevice constructor
this.processor = processor;
this.ramGB = ramGB;
this.cellularConnected = false;
System.out.println("๐Ÿ“ฑ SmartPhone(String, String, String, int) constructor");
System.out.println("   Processor: " + processor + ", RAM: " + ramGB + "GB");
}
public void makeCall(String number) {
if (cellularConnected) {
System.out.println("Calling " + number + " from " + deviceName);
} else {
System.out.println("Cannot call - no cellular connection");
}
}
public void connectCellular() {
this.cellularConnected = true;
System.out.println(deviceName + " connected to cellular network");
}
@Override
public void displayInfo() {
super.displayInfo(); // Call SmartDevice displayInfo
System.out.println("   Processor: " + processor);
System.out.println("   RAM: " + ramGB + "GB");
System.out.println("   Cellular: " + (cellularConnected ? "Connected" : "Disconnected"));
}
}
class GamingPhone extends SmartPhone {
private String gamingFeature;
private boolean gamingMode;
public GamingPhone(String deviceName, String osVersion, String processor, int ramGB, String gamingFeature) {
super(deviceName, osVersion, processor, ramGB); // Call SmartPhone constructor
this.gamingFeature = gamingFeature;
this.gamingMode = false;
System.out.println("๐ŸŽฎ GamingPhone(String, String, String, int, String) constructor");
System.out.println("   Gaming Feature: " + gamingFeature);
}
public void enableGamingMode() {
this.gamingMode = true;
System.out.println(deviceName + " gaming mode ENABLED - " + gamingFeature + " activated!");
}
public void disableGamingMode() {
this.gamingMode = false;
System.out.println(deviceName + " gaming mode DISABLED");
}
@Override
public void displayInfo() {
super.displayInfo(); // Call SmartPhone displayInfo
System.out.println("   Gaming Feature: " + gamingFeature);
System.out.println("   Gaming Mode: " + (gamingMode ? "ENABLED" : "DISABLED"));
}
public void playGame(String gameName) {
if (gamingMode) {
System.out.println("Playing " + gameName + " in enhanced mode with " + gamingFeature);
} else {
System.out.println("Playing " + gameName + " in normal mode");
}
}
}

Output:

=== COMPLEX SUPER() SCENARIOS ===
Creating SmartDevice:
๐Ÿ“ฑ Device(String) constructor
Device: Generic Device, Serial: SN1705300000000
๐Ÿ“Ÿ SmartDevice(String, String) constructor
OS: 1.0
Creating SmartPhone:
๐Ÿ“ฑ Device(String) constructor
Device: iPhone 15, Serial: SN1705300001000
๐Ÿ“Ÿ SmartDevice(String, String) constructor
OS: iOS 17
๐Ÿ“ฑ SmartPhone(String, String, String, int) constructor
Processor: A17 Pro, RAM: 8GB
Creating GamingPhone:
๐Ÿ“ฑ Device(String) constructor
Device: ROG Phone 7, Serial: SN1705300002000
๐Ÿ“Ÿ SmartDevice(String, String) constructor
OS: Android 13
๐Ÿ“ฑ SmartPhone(String, String, String, int) constructor
Processor: Snapdragon 8 Gen 2, RAM: 16GB
๐ŸŽฎ GamingPhone(String, String, String, int, String) constructor
Gaming Feature: AirTrigger
=== DEVICE CAPABILITIES ===
Device: Generic Device (SN1705300000000)
Powered: OFF
OS Version: 1.0
Internet: Disconnected
Device: iPhone 15 (SN1705300001000)
Powered: OFF
OS Version: iOS 17
Internet: Disconnected
Processor: A17 Pro
RAM: 8GB
Cellular: Disconnected
Device: ROG Phone 7 (SN1705300002000)
Powered: OFF
OS Version: Android 13
Internet: Disconnected
Processor: Snapdragon 8 Gen 2
RAM: 16GB
Cellular: Disconnected
Gaming Feature: AirTrigger
Gaming Mode: DISABLED

Example 5: Common Pitfalls and Best Practices

public class SuperPitfalls {
public static void main(String[] args) {
System.out.println("=== COMMON PITFALLS AND BEST PRACTICES ===");
// Pitfall 1: super() not first statement
System.out.println("1. SUPER() POSITION PITFALL:");
// ProblematicClass problem = new ProblematicClass(); // Would not compile
// Best Practice 1: Proper super() usage
System.out.println("\n2. PROPER SUPER() USAGE:");
ProperClass proper = new ProperClass("Test Data", 42);
// Pitfall 2: No default parent constructor
System.out.println("\n3. NO DEFAULT PARENT CONSTRUCTOR:");
// ChildNoDefault child = new ChildNoDefault(); // Would not compile
// Best Practice 2: Providing necessary constructors
System.out.println("\n4. PROVIDING NECESSARY CONSTRUCTORS:");
ChildWithDefault childWithDefault = new ChildWithDefault();
ChildWithDefault childWithParams = new ChildWithDefault("Custom Data");
// Best Practice 3: Constructor chaining with this() and super()
System.out.println("\n5. CONSTRUCTOR CHAINING:");
ChainedClass chained = new ChainedClass();
ChainedClass chainedWithParam = new ChainedClass("Parameter");
}
}
// โŒ PITFALL 1: super() not first statement
class ProblematicClass extends ParentClass {
private String data;
/*
public ProblematicClass(String data) {
this.data = data; // โŒ ERROR: super() must be first statement
super();          // This would cause compilation error
}
*/
}
// โœ… BEST PRACTICE 1: super() as first statement
class ProperClass extends ParentClass {
private String data;
private int value;
public ProperClass(String data, int value) {
super("Proper Parent Data"); // โœ… super() first statement
this.data = data;
this.value = value;
System.out.println("โœ… ProperClass constructor: " + data + ", " + value);
}
}
class ParentClass {
protected String parentData;
public ParentClass(String data) {
this.parentData = data;
System.out.println("๐Ÿ‘จ ParentClass constructor: " + data);
}
// โŒ No default constructor - only parameterized
// This affects child classes
}
// โŒ PITFALL 2: Child class with no matching parent constructor
class ChildNoDefault extends ParentClass {
/*
public ChildNoDefault() {
// โŒ ERROR: No default constructor in ParentClass
// Java can't call super() automatically
}
*/
// The only way is to call the parameterized constructor
public ChildNoDefault(String data) {
super(data); // โœ… Must call existing parent constructor
}
}
// โœ… BEST PRACTICE 2: Providing necessary constructors
class ChildWithDefault extends ParentClass {
private String childData;
// This works because we call the parameterized parent constructor
public ChildWithDefault() {
super("Default from Child"); // โœ… Provide default value for parent
this.childData = "Child Default";
System.out.println("๐Ÿ‘ถ ChildWithDefault() constructor");
}
public ChildWithDefault(String childData) {
super("Parent for: " + childData); // โœ… Call parent with related data
this.childData = childData;
System.out.println("๐Ÿ‘ถ ChildWithDefault(String) constructor: " + childData);
}
}
// โœ… BEST PRACTICE 3: Constructor chaining with this() and super()
class ChainedClass extends ParentClass {
private String data;
private int number;
private boolean flag;
// Default constructor
public ChainedClass() {
this("Default Data", 0); // โœ… Call another constructor in same class
System.out.println("๐Ÿ”„ ChainedClass() - default completed");
}
// Single parameter constructor
public ChainedClass(String data) {
this(data, 100); // โœ… Call two-parameter constructor
System.out.println("๐Ÿ”„ ChainedClass(String) - single param completed");
}
// Two parameter constructor - this one calls super()
public ChainedClass(String data, int number) {
super("Chained: " + data); // โœ… super() must be first in this constructor
this.data = data;
this.number = number;
this.flag = true;
System.out.println("๐Ÿ”„ ChainedClass(String, int) - two params completed");
}
public void display() {
System.out.println("Data: " + data + ", Number: " + number + ", Flag: " + flag);
System.out.println("Parent Data: " + parentData);
}
}
// โœ… BEST PRACTICE 4: Complex initialization with helper methods
class ComplexClass extends ParentClass {
private String[] dataArray;
private int size;
public ComplexClass(String initialData) {
super(processInitialData(initialData)); // โœ… Can use method calls in super()
this.dataArray = initializeArray(initialData);
this.size = dataArray.length;
System.out.println("โœ… ComplexClass created with size: " + size);
}
private static String processInitialData(String data) {
return "Processed: " + (data != null ? data.toUpperCase() : "NULL");
}
private String[] initializeArray(String data) {
if (data == null) return new String[0];
return data.split(" ");
}
}

Output:

=== COMMON PITFALLS AND BEST PRACTICES ===
1. SUPER() POSITION PITFALL:
2. PROPER SUPER() USAGE:
๐Ÿ‘จ ParentClass constructor: Proper Parent Data
โœ… ProperClass constructor: Test Data, 42
3. NO DEFAULT PARENT CONSTRUCTOR:
4. PROVIDING NECESSARY CONSTRUCTORS:
๐Ÿ‘จ ParentClass constructor: Default from Child
๐Ÿ‘ถ ChildWithDefault() constructor
๐Ÿ‘จ ParentClass constructor: Parent for: Custom Data
๐Ÿ‘ถ ChildWithDefault(String) constructor: Custom Data
5. CONSTRUCTOR CHAINING:
๐Ÿ‘จ ParentClass constructor: Chained: Default Data
๐Ÿ”„ ChainedClass(String, int) - two params completed
๐Ÿ”„ ChainedClass() - default completed
๐Ÿ‘จ ParentClass constructor: Chained: Parameter
๐Ÿ”„ ChainedClass(String, int) - two params completed
๐Ÿ”„ ChainedClass(String) - single param completed

Super() Rules and Constraints

RuleDescriptionExample
First statementsuper() must be first statement in constructorsuper(); this.data = data;
Implicit callJava adds super() automatically if not specifiedNo constructor โ†’ auto super()
Explicit requiredMust call super() explicitly if parent has no default constructorsuper("data");
Constructor chainingCan chain this() and super() but super() must be ultimate first callthis(); โ†’ eventually calls super()
Parameter matchingMust match parent constructor signaturesuper(name, age)

Best Practices

  1. Always call super() explicitly when parent has parameterized constructors
  2. Place super() as first statement in child constructors
  3. Provide default constructors in base classes if extensibility is expected
  4. Use meaningful parameters when calling parent constructors
  5. Document constructor requirements for subclasses
  6. Consider making classes final if they shouldn't be extended
  7. Use constructor chaining to avoid code duplication

Common Patterns

// Pattern 1: Base class with protected constructor for subclasses
public abstract class BaseEntity {
protected final String id;
protected final long createdAt;
protected BaseEntity(String id) {
this.id = id;
this.createdAt = System.currentTimeMillis();
}
}
// Pattern 2: Builder pattern with super() calls
public class Product {
private final String name;
private final double price;
protected Product(Builder builder) {
this.name = builder.name;
this.price = builder.price;
}
public static class ElectricProduct extends Product {
private final int warrantyMonths;
protected ElectricProduct(ElectricBuilder builder) {
super(builder); // Call parent constructor
this.warrantyMonths = builder.warrantyMonths;
}
}
}
// Pattern 3: Template method pattern with super()
public abstract class PaymentProcessor {
protected final String processorName;
protected PaymentProcessor(String name) {
this.processorName = name;
}
}
public class PayPalProcessor extends PaymentProcessor {
public PayPalProcessor() {
super("PayPal"); // Provide specific name to parent
}
}

Conclusion

The super keyword in constructors is Java's inheritance enforcer that ensures proper object initialization:

  • โœ… Constructor chaining: Guarantees parent initialization before child
  • โœ… Flexible parameter passing: Can pass data to parent constructors
  • โœ… Inheritance integrity: Maintains proper class hierarchy
  • โœ… Code reuse: Leverages parent class initialization logic

Key Takeaways:

  • super() must be first statement in child constructors
  • Java provides super() automatically if no parent constructor is called
  • Must call super() explicitly when parent has no default constructor
  • Constructor parameters can be passed to parent
  • Proper super() usage ensures robust inheritance hierarchies

The super keyword is essential for building well-structured, maintainable class hierarchies in Java, ensuring that every object is properly initialized according to its position in the inheritance chain!

Leave a Reply

Your email address will not be published. Required fields are marked *


Macro Nepal Helper