Sub topic 1
Subclasses and Inheritance: The Starting Point
Polymorphism in Java grows out of one simpler idea: inheritance. This chapter covers what inheritance is, how to write it, and what a subclass gets from its parent.
What Inheritance Means
A class is a blueprint for objects. Inheritance lets one class reuse the fields and methods of another class.
- A superclass (also called a parent class) is the class being reused.
- A subclass (also called a child class) is the new class that builds on it.
A subclass automatically has the superclass's fields and methods. It can also add new ones of its own.
This matters because you write shared code once. If every animal has a name and can eat, you put that in one place. Each specific animal only adds what makes it different.
The extends Keyword
In Java, you create a subclass with the extends keyword. class Dog extends Animal means "Dog is a subclass of Animal."
Here is a complete program you can run. Save it as Main.java.
class Animal {
String name;
Animal(String name) {
this.name = name;
}
void eat() {
System.out.println(name + " is eating.");
}
}
class Dog extends Animal {
Dog(String name) {
super(name);
}
void fetch() {
System.out.println(name + " fetches the ball.");
}
}
public class Main {
public static void main(String[] args) {
Dog rex = new Dog("Rex");
rex.eat();
rex.fetch();
}
}Compile and run it:
javac Main.java
java MainThe output is:
Rex is eating.
Rex fetches the ball.Dog never defines eat() or name, yet rex can use both. They come from Animal. The fetch() method is new, and only dogs have it.
The "Is-A" Test
Use inheritance only when the subclass really is a kind of the superclass. A dog is an animal, so Dog extends Animal makes sense. A car has an engine, but a car is not an engine, so Car extends Engine would be wrong.
| Relationship | Example | Use inheritance? |
|---|---|---|
| is-a | A Dog is an Animal | Yes |
| has-a | A Car has an Engine | No, use a field instead |
A Family of Classes
One superclass can have many subclasses. Each one shares the parent's code and adds its own.
A few rules to remember:
- A Java class can extend only one class directly.
- Every class that doesn't say
extendsstill has a parent: the built-inObjectclass. - Inheritance can go several levels deep. For example,
Puppy extends Dogmakes Puppy inherit from both Dog and Animal.
So far each subclass has only added methods. Later chapters show how a subclass can replace an inherited method with its own version. That technique, called overriding, is what makes polymorphism work.
Exercise
Write a class Cat that extends the Animal class above, with a constructor that takes a name and a method climb() that prints "<name> climbs a tree." Then explain why new Cat("Tom").eat() works even though Cat has no eat() method.
Show answer
class Cat extends Animal { Cat(String name) { super(name); } void climb() { System.out.println(name + " climbs a tree."); } } The call works because Cat extends Animal, so Cat inherits the eat() method and the name field. It prints "Tom is eating."
Exercise
Should a class Square extend a class Color? Use the is-a test to decide.
Show answer
No. A square is not a kind of color. A square might have a color, so Color should be a field inside Square, not its superclass.