Inheritance*

What is inheritance?

If you have written a class with methods and properties, a different class can be give all of those methods and properties with one simple command.  Any changes to the methods and properties of the parent class will be instantly passed on to the class that is inheriting them. The inheriting class can also be given extra properties and methods all of its own.

In this task your will write the code for an Employee class. The Employee class will inherit all of the methods and properties of the Person class, and will have some extra properties specific to an Employee including JobRole and Salary.

Write the Employee class

You will start by writing the Employee class with Employee specific properties.

Steps to follow:

  1. Write an Employee class inside your Personnel solution as shown here (the code can be written underneath the existing Person class):

  1. Rebuild your Personnel solution.
  2. In your front end, instantiate new Employee object with the name emp1. Assign values to the JobRole and Salary properties and output them as sown here. Notice that only Employee properties are available:

Inherit from Person

An Employee is a type of Person, so this class can now be modified to inherit the properties and methods of the Person class.

Steps to follow:

  1. Type the the Inherits keyword at the top of your Employee class as shown here:

  1. Rebuild your Personnel solution.
  2. In your front end, notice that the Employee now has the properties and methods of a Person:

  1. Assign some values to these inherited properties for emp1 and output them as shown here:

NOTE:
It is still possible to create a Person object directly from the person class, and it will have its own original interface.

Write another method

You will implement a method in the Person class with the name GetInitials, to obtain the initials of a person from their first name and last name. This new method will automatically be will be inherited by the Employee.

Steps to follow:

  1. Write a new method on the Person class with the name GetInitials as follows:

  1. Rebuild your Personnel solution.
  2. Modify your front end to call the GetInitials method of your Employee emp1. Notice that the method simply gets the fist letters of the first name and surname.

NOTE:
When one class inherits from another, the class that was inherited from is known as the super-class or the parent class, the class that did the inheriting is known as the sub-class or the derived class.

Override an inherited method

You will now override the GetInitials method that was inherited by the Employee class with a different version that includes the first letter of the job role as well.

Steps to follow:

  1. Modify the GetInitials method that you wrote in the Person class so that it can be overridden by the Employee class as shown here:

  1. Write another method inside the Employee class, also with the name GetInitials, but use the Overrides keyword to indicate that this is a replacement for the method that was inherited. This new version of the method also includes the first letter of the job role:

  1. Rebuild your Personnel solution.
  2. In your front end, call the GetInitials method for a Person object, and also call it for an Employee object as shown here. Notice that method returns a different result for each type of object:

NOTE:
The ability to override an inherited method allows different objects to implement the same method in different ways. Here we have different classes with a common interface, but they have different functionality for this interface. This fundamental concept of object oriented programming is known as polymorphism.

Multi-level Inheritance

An Employee is a type of Person, but there are also types of Employee. For example, a Manager is a type of Employee and so is an Administrator.  You will now code another class that will inherit all of the methods and properties of the Employee class.

Steps to follow:

  1. Code up another class in your Personnel solution called Manager.
  2. Type Inherits Employee at the top of the Manager class.
  3. Write code for a Department property in the Manager class.
  4. Instantiate a new Manager object in your front end and notice that it has all the properties and methods of an Employee (which includes those the Employee inherited from the Person.
  5. Code up another class in your Personnel solution called Administrator.
  6. Add code so the Administrator class inherits from Employee.
  7. Write code for a Location property in the Administrator class.

NOTE:
When you have a chain of inheritance like this, the super-class at the bottom of the hierarchy (in this case the Person class) is known as the base class.

Constructors and inheritance

When one class inherits from another, all of the properties and methods are inherited by the sub-class, except the constructor method. The constructor is a special case. If you want a sub-class to have the same customised constructor of its super-class, you must write it again inside the sub-class. The sub-class constructor can call its super-class’s constructor if appropriate, by using the keyword MyBase.

Steps to follow:

  1. Write a customised constructor for your Employee class. This should allow all of the Employee’s properties to be passed when an Employee object is instantiated. The Employee constructor should call it’s superclass’s constructor. You should also include an overload for the Employee constructor that does not expect parameters as shown here:

  1. Rebuild your Personnel solution and try out the Employee constructor as shown here:

Extension task

A base class can be written in such as way as you cannot directly instantiate an object from it, you can only inherit from it. This allows you to define functionality that is common to all the classes derived from a base class. This saves the derived classes from having to redefine the common methods and properties. In some cases, this common functionality is not complete enough to make a usable object, so each derived class can define the missing functionality. Such as base class is known as an abstract base class. To define an abstract base class you should use the MustInherit keyword in its class definition.

Start a new project of type Class Library called Geometry. Define classes inside this solution as follows. Then test these with a new front end.  Notice that you can instantiate a circle object or a square object, but you cannot instantiate a shape object.

NOTE:
We can create two different objects from these classes, a circle and a square, and they share a common interface; they both have a method called area.  But each area method works in a different way.  This is another example of polymorhism.