super Keyword

Accessing parent class members

5 min read

super Keyword in Java

The super keyword refers to the parent class. It's used to access parent members and call parent constructors.

Uses of super

  • super(): Call parent constructor (must be first line)
  • super.method(): Call parent's method
  • super.field: Access parent's field (if hidden)

🔑 Important: super() must be the FIRST statement in constructor. If not explicit, compiler adds super() automatically.

Code Examples

Calling parent constructor with super()

java
1// super() - Calling Parent Constructor
2public class Animal {
3    protected String name;
4    
5    public Animal() {
6        System.out.println("Animal no-arg constructor");
7    }
8    
9    public Animal(String name) {
10        this.name = name;
11        System.out.println("Animal(String) constructor");
12    }
13}
14
15public class Dog extends Animal {
16    private String breed;
17    
18    public Dog() {
19        // super() is called automatically - no-arg
20        System.out.println("Dog no-arg constructor");
21    }
22    
23    public Dog(String name, String breed) {
24        super(name);  // Explicit call - MUST be first line!
25        this.breed = breed;
26        System.out.println("Dog(String, String) constructor");
27    }
28}
29
30// Output for new Dog():
31// Animal no-arg constructor
32// Dog no-arg constructor
33
34// Output for new Dog("Buddy", "Labrador"):
35// Animal(String) constructor
36// Dog(String, String) constructor

Calling parent methods with super.method()

java
1// super.method() - Calling Parent Method
2public class Vehicle {
3    public void start() {
4        System.out.println("Vehicle: Starting engine");
5    }
6    
7    public void stop() {
8        System.out.println("Vehicle: Stopping engine");
9    }
10}
11
12public class Car extends Vehicle {
13    @Override
14    public void start() {
15        // Call parent's version first
16        super.start();
17        // Then add child's behavior
18        System.out.println("Car: Checking seatbelt");
19        System.out.println("Car: Ready to drive");
20    }
21    
22    // Can call super from any method, not just overridden ones
23    public void emergencyStop() {
24        System.out.println("Car: Emergency!");
25        super.stop();  // Delegate to parent
26    }
27}
28
29Car car = new Car();
30car.start();
31// Vehicle: Starting engine
32// Car: Checking seatbelt
33// Car: Ready to drive

Accessing hidden parent fields

java
1// super.field - Accessing Hidden Fields
2public class Parent {
3    protected String name = "Parent Name";
4    protected int value = 10;
5}
6
7public class Child extends Parent {
8    protected String name = "Child Name";  // Hides parent's name
9    
10    public void display() {
11        System.out.println(name);        // Child Name
12        System.out.println(this.name);   // Child Name
13        System.out.println(super.name);  // Parent Name
14        
15        // value is not hidden, accessed normally
16        System.out.println(value);       // 10
17        System.out.println(super.value); // 10 (same thing)
18    }
19}
20
21// Note: Hiding fields is generally bad practice!
22// Prefer accessing via methods for proper polymorphism

Common patterns using super

java
1// Common Pattern: Extending Functionality
2public class Logger {
3    public void log(String message) {
4        System.out.println("[LOG] " + message);
5    }
6}
7
8public class TimestampLogger extends Logger {
9    @Override
10    public void log(String message) {
11        String timestamp = LocalDateTime.now().toString();
12        // Enhance parent's behavior
13        super.log("[" + timestamp + "] " + message);
14    }
15}
16
17// Another Pattern: Template with super
18public abstract class DataProcessor {
19    protected void process() {
20        validate();
21        transform();
22        save();
23    }
24    
25    protected void validate() {
26        System.out.println("Default validation");
27    }
28    
29    protected abstract void transform();
30    protected abstract void save();
31}
32
33public class CSVProcessor extends DataProcessor {
34    @Override
35    protected void validate() {
36        super.validate();  // Run default validation first
37        System.out.println("CSV-specific validation");
38    }
39    
40    @Override
41    protected void transform() { /* CSV transform */ }
42    
43    @Override
44    protected void save() { /* Save CSV */ }
45}

Use Cases

  • Calling parent constructors
  • Extending parent method behavior
  • Accessing hidden fields
  • Template method pattern
  • Decorator-like extensions
  • Initialization chains

Common Mistakes to Avoid

  • super() not as first statement
  • Calling super() in a method (only constructors)
  • Using super when not needed
  • Confusing super with this
  • Not calling super constructor when needed
  • Hiding fields instead of using methods