super() in constructors
java
public class Animal {
private String name;
private int age;
public Animal(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() { return name; }
public int getAge() { return age; }
}
public class Dog extends Animal {
private String breed;
public Dog(String name, int age, String breed) {
super(name, age); // MUST be first line — calls Animal constructor
this.breed = breed; // then initialize Dog-specific fields
}
public String getBreed() { return breed; }
}
java
Dog d = new Dog("Rex", 5, "Labrador");
System.out.println(d.getName()); // "Rex" (inherited from Animal)
System.out.println(d.getBreed()); // "Labrador" (Dog-specific)
Rules for super()
What happens if you forget super()?
java
public class Animal {
private String name;
public Animal(String name) { // only parameterized constructor
this.name = name;
}
}
public class Cat extends Animal {
public Cat() {
// Java inserts super() here — but Animal has no no-arg constructor!
// COMPILE ERROR!
}
}
java
public class Cat extends Animal {
public Cat() {
super("Unknown"); // explicitly call parent's constructor
}
}
Constructor chaining
java
public class Vehicle {
public Vehicle() {
System.out.println("Vehicle constructor");
}
}
public class Car extends Vehicle {
public Car() {
super(); // calls Vehicle()
System.out.println("Car constructor");
}
}
public class SUV extends Car {
public SUV() {
super(); // calls Car()
System.out.println("SUV constructor");
}
}
SUV s = new SUV();
Vehicle constructor
Car constructor
SUV constructor
super.method() in overridden methods
java
public class Employee {
private String name;
private double salary;
public Employee(String name, double salary) {
this.name = name;
this.salary = salary;
}
public String getDetails() {
return name + " - $" + salary;
}
}
public class Manager extends Employee {
private String department;
public Manager(String name, double salary, String dept) {
super(name, salary);
this.department = dept;
}
@Override
public String getDetails() {
// Reuse parent's implementation, then add more
return super.getDetails() + " (" + department + " dept)";
}
}
java
Manager m = new Manager("Alice", 85000, "Engineering");
System.out.println(m.getDetails());
// Alice - $85000.0 (Engineering dept)
Accumulating behavior pattern
java
public class Shape {
public String describe() {
return "Shape";
}
}
public class Rectangle extends Shape {
private int width, height;
public Rectangle(int w, int h) {
width = w; height = h;
}
@Override
public String describe() {
return super.describe() + " -> Rectangle(" + width + "x" + height + ")";
}
}
public class Square extends Rectangle {
public Square(int side) {
super(side, side);
}
@Override
public String describe() {
return super.describe() + " -> Square";
}
}
Square s = new Square(5);
System.out.println(s.describe());
// Shape -> Rectangle(5x5) -> Square
super vs. this
java
public class Student extends Person {
private int grade;
public Student(String name, int grade) {
super(name); // call Person's constructor
this.grade = grade; // refer to this object's field
}
}
Cannot chain super.super
java
public class C extends B {
public void method() {
super.method(); // OK — calls B's version
// super.super.method(); // COMPILE ERROR — can't skip to A
}
}
Complete example: Game character hierarchy
java
public class GameCharacter {
private String name;
private int health;
public GameCharacter(String name, int health) {
this.name = name;
this.health = health;
}
public String getName() { return name; }
public int getHealth() { return health; }
public String toString() {
return name + " (HP: " + health + ")";
}
}
public class Warrior extends GameCharacter {
private int armor;
public Warrior(String name, int health, int armor) {
super(name, health); // initialize name and health via parent
this.armor = armor;
}
@Override
public String toString() {
return super.toString() + " [Armor: " + armor + "]";
}
}
public class Paladin extends Warrior {
private int holyPower;
public Paladin(String name, int health, int armor, int holy) {
super(name, health, armor); // calls Warrior constructor
this.holyPower = holy;
}
@Override
public String toString() {
return super.toString() + " {Holy: " + holyPower + "}";
}
}
java
Paladin p = new Paladin("Arthur", 100, 50, 30);
System.out.println(p);
// Arthur (HP: 100) [Armor: 50] {Holy: 30}
AP Exam Tips
- •
- •
- •
- •
- •
- •