Inheritance and polymorphism

Object Oriented basics

We learned previously that OOP is a different approach to creating programs:

  • data and code is bundled together in classes
  • real world entities are modelled as classes
  • data and code can be hidden using the private keyword
  • classes can be used as templates to create objects
  • objects can interact with other objects to form a more complex program
  • data can be changed in an object via a set method
  • data can be accessed from an object via a get method
  • a class can have a special method that returns no value and has the same name as the class called a constructor
  • constructors are called automatically when a new object is created from a class using the keyword new

Inheritance

The next significant concept in OO is the idea of inheritance. You as an individual inherited 50% of your DNA from your mother, and 50% of your DNA from your father. As a result some aspects of your physical characteristics are inherited from each of those parents, for example hair colour, or height, or maybe very specific traits like long fingers.

Classes can make use of inheritance whereby classes inherit code and data from their base class.

The Vehicle class will contain data and code that are inherited by the Car and Boat classes

Imagine we were writing a game like Grand Theft Auto. The player rides around in vehicles and can interact with (i.e. crash into!) other vehicles. This is a great application for classes with inheritance. So a Car class will be based on a Vehicle and will inherit all its methods and attributes. The same goes for Boat.

Ask yourself, in the game and therefore class:

  • What properties might the Vehicle base class have (e.g. colour)?
  • What extra properties might a car or boat have in addition to those of a generic vehicle?

In Java, inheritance is enabled by the extends keyword.

public Class Car extends Vehicle{
    // additional properties and methods are defined here
}

Hierachy

Classes can be arranged in a hierarchy, where one sub class of a base class might have further sub classes, in this case a family car is a type of car, which is a type of vehicle.

Inherited classes can themselves be inherited by other classes

Note the many names we use in OO with subtly different meanings:

  • parent/base/super/ancestor classes
  • child/sub/descendant classes

Polymorphism

Polymorphism is a way to allow sub classes to behave differently from the base class. Both the car and the boat objects might have move() methods. However, is the way they move the same? Probably not, if the vehicle class has a move() method, it can be replaced by a new move() method within the car class, and a different one again in the boat class.

Vehicles might have different ways of behaving when performing the same action. A car doesn’t move like a boat for example.

The most common way of implementing polymorphism is through over riding.

A method in the base class vehicle can be replaced with a new method of exactly the same name by one of the child classes.

The inherited method is overridden by the method in the child class 

Another way of implementing polymorphism is by providing many different versions of a method, each with a different number and type of arguments. This is called overloading, because we can potentially add to a class lots of methods with the same name, but with slightly different behaviour.

int myMethod(){ // no arguments
    ... 
}
int myMethod(char c){ // accepts a char argument
    ...
}
int myMethod(char c, int n){ // accepts a char and int arg
    ...
}
int myMethod(int x, int y){ // accepts two int arguments
    ...
}

It is common to provide overloaded constructors in a class. Only one of them will run when an object is created. Which one is called is dependant on how many arguments were passed to it.

Protected

You will recall the keywords public and private which control access to attributes and methods within a class. To that we can now add a third: protected which only allows access to classes within an inheritance hierarchy.

I like to remember it as a halfway house keyword between private and public that says “keep it in the family”.

private int ID; // only members of its class can access
public setName(String Name){ // any class can access
protected getName(){ // inherited classes can access

super

An inherited class may want to call the constructor or another method from its parent/super class. This can be done using the super keyword. Used with brackets, it calls the constructor, optionally passing on any parameters needed by the super class constructor. If done using dot notation, it can call a method from the base class

super("Alex", 1234); // calls the constructor of the parent class, passing a string and integer to it.

super.moveLeft(); // calls a method in the parent class that has been overridden by this class.

this

You may remember the keyword this which is used to refer to the current object.

this can be used in a constructor to call other constructors in the same class.

class Person(){
  private String name; // instance variables
  private int ID;

  Person(){ //default constructor
    this("No name set",0);
  }
  Person(String studentName, int studentID){ //constructor
    name=studentName;
    ID=studentID;
  } 
  ...

Note: Because super and this must always be in the first line of a constructor, you cannot use both at the same time.

Activity

To consolidate your understanding of the many concepts introduced above, you are going to create a program that can simulate a library.

Download the file here and extract the files to a folder on your own computer.

Download and complete the steps in the document below.

Knowledge check

  • What can a class inherit?
  • What is an alternative name for a parent class?
  • What is polymorphism?
  • How can you implement polymorphism in OOP?
  • How is it possible for a single class to have multiple constructors?
  • What is super used for?

Next lesson