Summary - Inheritance
- Java course
- Intermediate
- Lesson 18/116
- 5 min
Combine super and override to write a shape hierarchy.
Summary - Inheritance
In this group you learned inheritance with extends, the superclass constructor call with super, method overriding with @Override, the Object class and the final keyword. Inheritance enables code sharing between classes.
Now you will bring it all together: define a superclass, let a subclass inherit with extends, call the constructor with super, override a method with @Override. This is the foundation of object-oriented design.
Practice Make Bird inherit from Animal
Define a class Animal with a String name field and an Animal(String n) constructor that assigns name = n;. Also add an empty void sound() method. Define Bird extends Animal whose Bird(String n) constructor starts with super(n). Override void sound() with @Override; the body writes System.out.println(name + " sings");. Put main inside Bird; create a Bird instance and call sound.
Starter code
// Define the Animal class
// Define Bird extends Animal with constructor and override
// Instantiate and call in mainSample solution
class Animal {
String name;
Animal(String n) {
name = n;
}
void sound() {
}
}
class Bird extends Animal {
Bird(String n) {
super(n);
}
@Override
void sound() {
System.out.println(name + " sings");
}
public static void main(String[] args) {
Bird b = new Bird("Sparrow");
b.sound();
}
}Expected output
Sparrow sings
What comes next?
You learned inheritance. In the next groups you will protect fields with encapsulation, work with class-level data using static members and define contracts with interfaces. These are the next layer of class design.
This group combined extends, super, @Override, Object and final. These are the foundation of class hierarchy. Next you will control field access with encapsulation; you will protect data with private fields and getters/setters.
Practice Define Vehicle with inheritance
Define a class Vehicle with a String brand field and a Vehicle(String b) constructor that assigns brand = b;. Define Car extends Vehicle whose Car(String b) constructor starts with super(b). Add a public String toString() method to Car returning brand. Put main inside Car; create a Car instance and print it with println.
Starter code
// Define the Vehicle class
// Define Car extends Vehicle with super and toString
// Instantiate and print in mainSample solution
class Vehicle {
String brand;
Vehicle(String b) {
brand = b;
}
}
class Car extends Vehicle {
Car(String b) {
super(b);
}
public String toString() {
return brand;
}
public static void main(String[] args) {
Car c = new Car("Toyota");
System.out.println(c);
}
}Expected output
Toyota
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Where must super() be in a subclass constructor?
- In the middle
- At the first line Correct answer
- At the last line
- Outside the class
Explanation: At the first line — super() must be first.
2 Add the override annotation
class Bird extends Animal {
____
void fly() {
System.out.println("Uçuyor");
}
}
class Bird extends Animal {
@Override
void fly() {
System.out.println("Uçuyor");
}
}
Explanation: @Override marks the method override.