Java Notes
Learn Java inheritance with clear examples — extends keyword, what gets inherited, types of inheritance, constructor chain, and when to use inheritance vs composition.
When you write a Dog class and a Cat class, both have a name, both breathe, both eat. Without inheritance you would write those same fields and methods twice. With inheritance, you write them once in an Animal class and let Dog and Cat reuse them.
Inheritance lets one class receive the fields and methods of another class. The class that gives is called the parent class (or superclass). The class that receives is called the child class (or subclass).
The relationship must be "is-a": a Dog is an Animal. A Car is a Vehicle. If the "is-a" sentence does not make sense, inheritance is probably the wrong tool.
The extends Keyword
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
Line by line:
class Dog extends Animal— Dog gets all non-private fields and methods of Animal.d.nameandd.ageare Animal's fields — Dog did not declare them but has them.d.breathe()andd.eat()are Animal's methods — Dog uses them without writing them.d.bark()is Dog's own method — Animal does not have it.
One class, two purposes: Animal defines common behaviour, Dog adds what is unique to dogs.
What Gets Inherited?
Not everything from the parent reaches the child. The access modifier decides.
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
Private members belong only to the class that declares them. No child can access them directly. Use protected when you want subclasses to access a field but outsiders cannot.
Types of Inheritance in Java
Single Inheritance
One parent, one child. The simplest form.
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
Multilevel Inheritance
A chain: A → B → C. B extends A, C extends B.
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
Dog inherits from Mammal which inherits from Animal. A Dog object has access to all three levels.
Hierarchical Inheritance
One parent, multiple children.
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
Both Circle and Rectangle get color and displayColor() from Shape. Each adds its own area() logic.
Multiple inheritance (two parents, one child) is NOT allowed in Java for classes. Java does not allow class Dog extends Animal, Pet. This prevents ambiguity when both parents have a method with the same name. Java allows multiple inheritance only through interfaces.
Constructors and Inheritance
Constructors are not inherited. But when a child object is created, the parent constructor always runs first.
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
Line by line:
super(name)— explicitly calls Animal's constructor. Must be the first statement in Dog's constructor.- If you do not write
super(...), Java automatically insertssuper()(no-argument). If Animal has no no-argument constructor, you get a compile error. - Order: Animal constructor runs first, then Dog constructor. Object is built from the parent down.
super to Access Parent Methods
When a child overrides a parent method, super.methodName() calls the original parent version.
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
super.describe() runs Animal's version first, then Dog adds its own line. Without super.describe(), the Animal version would never run — Dog's override would completely replace it.
Inheritance vs Composition
Inheritance is powerful but overused. Use it only when the "is-a" relationship is genuinely true.
When the relationship is "has-a" — a Car has an Engine — use composition: give the Car class an Engine field.
// WRONG use of inheritance — a Car is NOT an Engine
public class Car extends Engine { // makes no sense
}
// CORRECT — composition
public class Engine {
void start() { System.out.println("Engine started"); }
}
public class Car {
Engine engine = new Engine(); // Car HAS-A Engine
void startCar() {
engine.start();
System.out.println("Car is ready.");
}
}Rule of thumb: if "X is a Y" sounds natural (Dog is an Animal, Manager is an Employee), use inheritance. If "X has a Y" sounds more accurate (Car has an Engine, House has a Room), use composition.
Common Mistake 1: Forgetting super() When Parent Has No Default Constructor
public class Animal {
String name;
public Animal(String name) { // parameterized — no default constructor
this.name = name;
}
}
public class Dog extends Animal {
// WRONG — Java inserts super() automatically, but Animal has no no-arg constructor
public Dog(String name) {
// compile error here
}
// CORRECT — explicitly call the parent constructor
public Dog(String name) {
super(name);
}
}Common Mistake 2: Trying Multiple Class Inheritance
// COMPILE ERROR — Java does not allow this
public class Mule extends Horse, Donkey {
}
// CORRECT approach — use interfaces for multiple type contracts
public class Mule extends Horse implements DonkeyBehavior {
}Practice Task
Task: Create a parent class Person with fields name (String) and age (int), and a method introduce() that prints "Hi, I am [name], age [age].". Create a child class Teacher that extends Person and adds a field subject (String). Add a method teach() that prints "[name] is teaching [subject].". In main, create a Teacher object and call both introduce() and teach().
Hint: Teacher's constructor should call super(name, age) to set the inherited fields. Then set this.subject.
Solution:
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
What to observe: t.introduce() uses name and age which came from Person. Try creating a Person object too — it can call introduce() but not teach(). A child can use parent methods, but a parent object cannot call child methods.
Continue the lesson path