Skip to content

Generalisation (Inheritance)

Estimated time to read: 4 minutes

Generalisation

We can model behaviours using methods. It lets us generalise behaviours and it eliminates the need to have identical code written throughout a program

Inheritance

Showing Inheritance in UML is done by connecting two classes with a solid lined arrow.

The superclass is at the head of the arrow and the subclass is at the tail.

In UML, it is standard practise to draw the diagram with the arrow pointing up. This way, superclasses are always towards the top, whilst subclasses are always towards the bottom.

Classes

A class is a blueprint for an object. Each unique instance of that class, shares the same attributes but has unique values associated with it.

Superclasses

Superclasses are generalised classes

Subclasses

Subclasses are specialised classes that inherit their base attributes and methods from the relevant superclass.

Subtyping

When modelling a problem, you can express subtyping between two types.

For example, a Dog is a subtype of Animal. Thus the Dog object is not only Dog-typed, but also Animal-typed.

The type signifies what these objects can do through public methods.

For example, instances of the Dog class are Dog-type objects.

Abstract Class

The use of abstract ensures that the class can not be instantiated directly. It must be instantiated through a subclass which then inherits the attributes and methods from the superclass.

public *abstract* class Animal {
 protected int numberOfLegs;
 protected int numberOfTails;
 protected String name;

 public Animal(String petName, int legs, int tails){
  this.name = petName;
  this.numberOfLegs = legs;
  this.numberOfTails = tails;
 }

 public void walk() {...}
 public void run() {...}
 public void eat() {...}
}

Extends (Inheritance)

The use of extends tells a class to inherit attributes and methods from the superclass.

public class Dog extends Animal {
 public Dog(String name, int legs, int tails){
  super(name, legs, tails);
 }
 public void playFetch(){...}
}

Constructors

Constructs instantiate classes. Doing new XYZ, the new keyword calls upon a constructor for that class.

When inheriting from a superclass, the subclass must give the superclass a chance to prepare attributes appropriately.

Classes can have either Implicit Constructors or Explicit Constructors.

Implicit Constructors

public abstract class Animal {
 protected int numberOfLegs;

 public void walk(){...}
}

Since the example above has no constructor method, all values are set to 0 or null. They are implicitly assigned.

Explicit Constructors

public abstract class Animal {
 protected int numberOfLegs;

 public Animal (int legs){
  this.numberOfLegs = legs;
 }
}

The example above has an explicit constructor, which means that the value can be set to whatever value is passed into the method arguments. These values are assigned to the superclass during instantiation.

Subclass Constructors

// Abstract / Super Class
public abstract class Animal {
 protected int numberOfLegs;

 // Explicit Constructor
 public Animal (int legs){
  this.numberOfLegs = legs;
 }
}

// Subclass of Animal
public class Dog extends Animal {
 public Dog(int legs){
  // Calls the Explicit Constructor from the Superclass
  super(legs);
 }
}

A subclass's constructor must call its superclass's constructor if the superclass has an explicit constructor.

Note! To access the superclasses attributes, methods and constructors, the subclass uses the keyword super.

Overrides

Subclasses can override the methods of its superclass. Meaning that a subclass can provide its own implementation for an inherited superclass' method.

Inheritance UML Example

Screenshot 2022-07-13 at 21.09.16.png

The above example shows two classes. Dog is a subclass of the superclass Animal. This means that Dog inherits the attributes and methods of the 'Animal' class, whilst also having its own methods appended.

Implementation Inheritance

For Java, only single implementation inheritance is allowed.

This means that a superclass can have multiple subclasses whilst a subclass can only inherit from one superclass.

For example, the Dog and Cat classes below, inherit from the Animal superclass. As a result, the Dog and Cat classes can not inherit from any other class. However, Animal can have an unlimited subclasses that inherit from it.

Screenshot 2022-07-14 at 10.32.36.png

Implementation Inheritance code example

public class Cat extends Animal {
 public Cat(String name, int legs, int tails){
  super(name, legs, tails);
 }
 public void playWithYarn(){...}
}

public class Dog extends Animal {
 public Dog(String name, int legs, int tails){
  super(name, legs, tails);
 }
 public void playFetch(){...}
}

Multiple Inheritance

This is when a subclass has two or more superclasses. This is explicitly not supported within Java, however, other languages such as C++ do have support for Multiple Inheritance.

Java does not support Multiple Inheritance, as inheriting from two or more superclasses can cause Data Ambiguity as it can not easily distinguish between the two superclasses if they the same name or method signatures.