Java Tutorial
Java Inheritance
A subclass reuses a superclass with extends. Write the shared parts once, then specialize — and call super when you need the parent version.
extends reuses a class
class Dog extends Animal means Dog has every non-private member of Animal, plus whatever you add. Inheritance is for an "is-a" relationship: a dog is an animal.
Example
class Animal {
void speak() {
System.out.println("...");
}
}
class Dog extends Animal {
@Override
void speak() {
System.out.println("woof");
}
}
public class Main {
public static void main(String[] args) {
new Dog().speak(); // woof
}
}super calls the parent
super.speak() runs the superclass method. A subclass constructor can call super(...)as its first statement to initialize the parent part of the object.
Example
class Animal {
String name;
Animal(String name) {
this.name = name;
}
void speak() {
System.out.println(name + " makes a sound");
}
}
class Dog extends Animal {
Dog(String name) {
super(name);
}
@Override
void speak() {
super.speak();
System.out.println(name + " barks");
}
}
public class Main {
public static void main(String[] args) {
new Dog("Mochi").speak();
}
}One superclass only
Java classes use single inheritance: one extends clause. For extra capabilities that are not a strict parent type, use interfaces. You can still build deep trees — Dog extends Animalextends Object — but each class has one direct parent.
Example — inherited fields
class Vehicle {
int speed;
}
class Bike extends Vehicle {
void accelerate() {
speed += 5;
}
}
public class Main {
public static void main(String[] args) {
Bike b = new Bike();
b.accelerate();
System.out.println(b.speed); // 5
}
}Inheritance couples types tightly. Prefer it when the subclass truly is a specialized version of the parent. For "has-a" (a car has an engine), use a field — composition — instead of extending.
Try It Yourself
Exercise: Create Cat extends Animal that overrides speak to print"meow". In main, call speak on both a Dog and aCat.
Show solution
class Animal {
void speak() { System.out.println("..."); }
}
class Dog extends Animal {
@Override void speak() { System.out.println("woof"); }
}
class Cat extends Animal {
@Override void speak() { System.out.println("meow"); }
}
public class Main {
public static void main(String[] args) {
new Dog().speak();
new Cat().speak();
}
}Both subclasses reuse the Animal type idea but supply their own speak behavior.
Key Takeaways
extendscreates an is-a relationship and reuses the superclass.- Override methods to specialize; mark with
@Override. superreaches the parent method or constructor.- Java allows one superclass; use interfaces for additional contracts.
Worked examples
The short listings above are there so you can see the grammar. The programs here use the same statements on quantities that already have units: a speed, a pH, a count of bases. They are classroom numbers. Air resistance is ignored. g is 9.81 m/s² unless a line says otherwise.
Open them in the Java editor at /java/try. Change one measurement and check whether the result still has the right unit.
Biology
A specialised animal
A subclass reuses a superclass. Dog is still an Animal, but speak is the specialised version.
Example
class Animal {
void speak() { System.out.println("..."); }
}
class Dog extends Animal {
@Override void speak() { System.out.println("woof"); }
}
public class Main {
public static void main(String[] args) {
new Dog().speak();
}
}