Creating a Class Hierarchy

Creating a class hierarchy in Java involves organizing classes in a hierarchical structure where classes inherit behaviors and properties from their parent classes. This is achieved using inheritance, where a subclass (or child class) inherits from a superclass (or parent class). Here’s a basic example:

				
					// Define a superclass
class Animal {
    String species;

    public Animal(String species) {
        this.species = species;
    }

    public void eat() {
        System.out.println("The " + species + " is eating.");
    }

    public void sleep() {
        System.out.println("The " + species + " is sleeping.");
    }
}

// Define subclasses
class Dog extends Animal {
    public Dog() {
        super("Dog");
    }

    public void bark() {
        System.out.println("Woof! Woof!");
    }
}

class Cat extends Animal {
    public Cat() {
        super("Cat");
    }

    public void meow() {
        System.out.println("Meow!");
    }
}

public class Main {
    public static void main(String[] args) {
        Dog dog = new Dog();
        Cat cat = new Cat();

        dog.eat();
        dog.sleep();
        dog.bark();

        cat.eat();
        cat.sleep();
        cat.meow();
    }
}

				
			

In this example:

  • Animal is the superclass, defining basic behaviors shared by all animals.
  • Dog and Cat are subclasses of Animal, inheriting its behaviors and properties.
  • Dog and Cat can also have their own specific behaviors (bark and meow, respectively).

When you run the Main class, it creates instances of Dog and Cat, demonstrating how inheritance works. The Dog and Cat objects can access methods defined in the Animal class because they inherit from it.

If you’re creating a larger set of classes for a very complex program, it makes sense for your classes not only to inherit from the existing class hierarchy, but also to make up a hierarchy themselves. This may take some
planning beforehand when you’re trying to figure out how to organize your Java code, but the advantages are significant once it’s done:
When you develop your classes in a hierarchy, you can factor out information common to multiple classes in superclasses, and then reuse that superclass’s information over and over again. Each subclass gets that
common information from its superclass.
Changing (or inserting) a class further up in the hierarchy automatically changes the behavior of its subclasses-no need to change or recompile any of the lower classes because they get the new information through
inheritance and not by copying any of the code.

For example, let’s go back to that Motorcycle class and pretend you created a Java program to implement all the features of a motorcycle. It’s done, it works, and everything is fine. Now, your next task is to create a
Java class called Car.

Car and Motorcycle have many similar features-both are vehicles driven by engines. Both have transmissions, headlamps, and speedometers. So your first impulse may be to open your Motorcycle class file and copy
over a lot of the information you already defined into the new class Car.

A far better plan is to factor out the common information for Car and Motorcycle into a more general class hierarchy. This may be a lot of work just for the classes Motorcycle and Car, but once you add Bicycle,
Scooter, Truck, and so on, having common behavior in a reusable superclass significantly reduces the amount of work you have to do overall.

Let’s design a class hierarchy that might serve this purpose. Starting at the top is the class Object, which is the root of all Java classes. The most general class to which a motorcycle and a car both belong might be called
Vehicle. A vehicle, generally, is defined as a thing that propels someone from one place to another. In the Vehicle class, you define only the behavior that enables someone to be propelled from point a to point b, and
nothing more.

Two-wheeled and four-wheeled vehicles.

Finally, with a subclass for the two-wheeled engine-powered vehicles, you can have a class for motorcycles. Alternatively, you could additionally define scooters and mopeds, both of which are two-wheeled
engine-powered vehicles but have different qualities from motorcycles.

Where do qualities such as make or color come in? Wherever you want them to go-or, more usually, where they fit most naturally in the class hierarchy. You can define the make and color on Vehicle, and all the
subclasses will have those variables as well. The point to remember is that you have to define a feature or a behavior only once in the hierarchy; it’s automatically reused by each subclass.

Scroll to Top