Explore inheritance and abstract classes

Completed

Inheritance and abstract classes are two important programming constructs that are found in object-oriented programming languages. These concepts are used to create parent-child relationships between classes. Inheritance allows a subclass to extend a super class. The subclass inherits all the attributes and methods of the super class. Subclasses can also override the behavior of or add more functionality to methods that are inherited from the super class.

Inheritance

To better understand the concept of inheritance in object-oriented programming, lets review a real-world example. In this example, Vehicle is the parent class. There are many different types of vehicles, for instance: cars, buses, and trucks. In this scenario, Vehicle is the parent class. Cars, buses, and trucks are derived classes that inherit from Vehicle. The Vehicle class has characteristics that are shared across all the vehicle types (speed, color, and gears for example). The characteristics for each of the vehicles types may be different. For instance, a truck might have all-wheel drive, but a car does not.

Diagram of the Inheritance from the Vehicle parent class to the car, bus, and truck derived classes.

Example

The following example demonstrates how a class uses inheritance. The Car class extends the Vehicle class to get the height and width but also adds the number of passengers variable.

class Vehicle
   {
	// Instance fields.
 		real height;
  		real width;
	// Constructor to initialize fields height and width.
	new(real _height, real _width)
   	{
    height = _height;
		  width = _width;
  	}
   }
class Car extends Vehicle
	{
	  // Additional instance field numberOfPassengers. Fields height and width are inherited.
 	  int numberOfPassengers;

	 // Constructor is overridden to initialize numberOfPassengers.
	 new(real _height, real _width, int _numberOfPassengers)
 	 {
	   // Initialize the fields.
	   super(_height, _width);
 	   numberOfPassengers = _numberOfPassengers;
 	 }
	}

Abstract

Abstract classes are identified by the keyword abstract. They cannot be instantiated but require a subclass that is inherited from the abstract class. Abstract classes are used to implement a concept that the subclass will then complete. Abstract methods can also be declared in abstract classes. Abstract methods do not allow code or declarations in the method. A subclass must extend the abstract class to use the abstract method.

Example

The following is an example of an abstract class. The Car class extends the abstract class Vehicle. This allows the Car class to override the printInfo() method and it allows you to add functionality to the operate() method that was not included in the abstract class.

abstract class Vehicle
{
      str owner;
      int age;

	void printInfo()
      {
    	Info(strfmt("%1, %2",owner, age));
      }
    abstract void operate()
    {
    }
 }
class Car extends Vehicle
{
     str model;
     void printInfo()
     {
            // overriding default functionality
            Info(strfmt("%1, %2, %3",owner, age, model));
     }
   void operate()
   {
        Info('running');
   }
}