Homesweet Learning helps students learn!
Inheritance

The idea of inheritance is simple but powerful: When you want to create a new class and there is already a class that includes some of the code that you want, you can derive your new class from the existing class. In doing this, you can reuse the fields and methods of the existing class without having to write (and debug!) them yourself.

A subclass inherits all the members (fields, methods, and nested classes) from its superclass. Constructors are not members, so they are not inherited by subclasses, but the constructor of the superclass can be invoked from the subclass.

In the Java language, classes can be derived from other classes, thereby inheriting fields and methods from those classes.

A class that is derived from another class is called a subclass (also a derived class, extended class, or child class). The class from which the subclass is derived is called a superclass (also a base class or a parent class).

Every class has one and only one direct superclass (single inheritance). In the absence of any other explicit superclass, every class is implicitly a subclass of Object. Classes can be derived from classes that are derived from classes that are derived from classes, and so on, and ultimately derived from the topmost class, Object.

An Example of Inheritance

Here is the sample code for a possible implementation of a Bicycle class:

public class Bicycle {

// the Bicycle class has three fields
public int cadence;
public int gear;
public int speed;

// the Bicycle class has one constructor
public Bicycle(int startCadence, int startSpeed, int startGear) {
   gear = startGear;
   cadence = startCadence;
   speed = startSpeed;
}

// the Bicycle class has four methods
public void setCadence(int newValue) {
   cadence = newValue;
}

public void setGear(int newValue) {
   gear = newValue;
}

public void applyBrake(int decrement) {
   speed -= decrement;
}

public void speedUp(int increment) {
   speed += increment;
}

}

A class declaration for a MountainBike class that is a subclass of Bicycle might look like this:

public class MountainBike extends Bicycle {

// the MountainBike subclass adds one field
public int seatHeight;

// the MountainBike subclass has one constructor
public MountainBike(int startHeight,
   int startCadence,
   int startSpeed,
   int startGear) {
   super(startCadence, startSpeed, startGear);
   seatHeight = startHeight;
}

// the MountainBike subclass adds one method
public void setHeight(int newValue) {
   seatHeight = newValue;
}
}

MountainBike inherits all the fields and methods of Bicycle and adds the field seatHeight and a method to set it. Except for the constructor, it is as if you had written a new MountainBike class entirely from scratch, with four fields and five methods. However, you didn't have to do all the work. This would be especially valuable if the methods in the Bicycle class were complex and had taken substantial time to debug.

What You Can Do in a Subclass

A subclass inherits all of the public and protected members of its parent, no matter what package the subclass is in. If the subclass is in the same package as its parent, it also inherits the package-private members of the parent. You can use the inherited members as is, replace them, hide them, or supplement them with new members:

Lab Write a java base class for Person that has defined methods for getting and setting age and sex attributes, and a method of eat, which takes 1 parameter of time of the day, and returns how much a person will eat based on the time of the day.
Write 2 sub-classes that both extends from the Person base class, one for Canadians, and one for Europeans. In these 2 sub-classes, "override" the eat method of the base Person class with different algorithm of calculating how much the Canadian or European will eat based on the time of the day.
Write another class called Doctor that has a method of telling if a person (Canadians or Europeans) will become overweight based on an average of how much the person eats at different times, his/her age and sex.