Inheritance

Inheritance allows a class to use the properties and methods of another class. The derived class will have the characteristics of the inherited class. A parrot and an eagle are classified as birds because they inherit common bird characteristics. Therefore, if we had a parrot class, an eagle class, and a bird class, the parrot and eagle class would be a subclass of the bird. The bird class in this case is the super-class. After the characteristics of the super-class (bird) are inherited, some changes can be made to the subclass (parrot/eagle) so that they will have slightly different characteristics. This is the basis of inheritance.

To inherit a class you simply have to add the ‘extends’ keyword to your class.

class Parrot extends Bird{
    …

Suppose we want to create three classes with the following characteristics:

A bird can fly and a fish can swim. Therefore, after inheriting the animal class, the fish class should add a swim function and the bird class should add a fly function.

Since the Bird class inheriting the Animal class…
The Bird class looks like this But acts like this

Let’s test the inheritance relationship we have built

Since the Fish has no fly method (fishes don’t fly), putting myFish.fly() in our code will cause an error.

If we decide to expand the inheritance relationship to include a sub-class of bird, it may look something like this.

Program: create an inheritance relationship between School Member, Staff and Student.
· School Member must have methods that allow a user to modify the age and name of a member.
· Staff must inherit School Member; additionally it should have an Employee # that can be modified.
· Student must inherit School member, additionally it should have a Course/Subject name that can be modified.