Inheritance

Dr Andy Evans

[Fullscreen]

Review

  • Classes are photocopy originals for objects.
  • Usually we make an object from a class.
  • The object contains all the code in the class.
  • We can therefore call methods inside the object.
    
    pointObject.setX(200.0);
    
    
  • We pass a method certain 'arguments'.
  • Can use object's variables directly but isn't usual.
    	
    pointObject.x = 200.0;
    
    

Philosophy of Object Orientation

  • Encapsulation.
    • Data and the methods working on them are contained.
    • You don't need to know how something is done, just what does it. Indeed, you shouldn't be able to find out.
    • Java: private/public, static, final, class, methods.
  • Polymorphism.
    • One idea should be presented in a flexible form.
    • Java: polymorphic methods.
  • Inheritance.
    • Classes should be able to inherit code from other classes and add their personal twist to it.

Inheritance

Say you go to the ATM of BlueBank Inc. It collects a PIN in an object.

 

But... you belong to RedBank Plc., which has its own software.

How does BlueBank send RedBank your details to confirm?

 

The object BlueBank has wasn't written by programmers at RedBank, so it is bound to be different from what they want.

 

BlueBank could have a class for every bank it deals with, but that would be complicated.

Instead, all the banks design a class description that has the information they need.

 

All banks promise the objects they send will match this class description as a minimum.

The objects can have other stuff as well, but they must match the description at the least.

Inheritance

BlueBankRequest
Methods:
getPin()
setPin()
...is a subtype of... GenericRequest
Methods demanded:
getPin()


There's nothing to stop RedBank also developing its own version with other methods along with getPin().

Methods

  • A methods can take a subclass object where it expects the superclass.
  • However, the method can then only use the superclass methods, and none of the subclass detail.
	
BlueBankRequest  b1 = 
		new BlueBankRequest();
b1.setPin (getAtmPin());
boolean pinOk = 
    redBankConnection.isPinOk(b1);

Because the isPinOk method takes in a GenericRequest and checks the things sent against that, it can guarantee that the getPin method will be there. As long as it just uses that, it will be fine. It doesn't need to know the object is more complicated.

	
boolean isPinOk (GenericRequest qr) {
    if (qr.getPin() == customerPin) {
		return true;
    } else { 
		return false;
    }
}

Inheritance

  • The getPin code in GenericRequest could be:
    1. full code that does the whole promised job;
    2. empty, with classes just promising the method will be there;
    3. something inbetween, with some methods full code, some just promises.

  • Java builds these in through:
    1. extension : whole code available + what you write
    2. interfaces : just a set of promises + you write everything
    3. abstract classes : mix of code and promises

Extension

Classes can be extended:
	
public class GenericRequest {
    protected int pin;
    public int getPin() {
		return pin;
    }
}

extends

  • Inheriting classes have all the public or protected variables and methods of the inherited class.
    	
    class BlueBankRequest extends GenericRequest {
        void setPin(int pinIn) {
    		pin = pinIn;
        }	
    }
    
    
  • Only need to define new variables and methods.
  • The pin variable and getPin are inherited from GenericRequest.
  • The parent class is called the Superclass, the inheritor is called the Subclass.

super

  • Subclasses can't access the private variables of Superclasses. Therefore, one way to set these is to use the Superclass's constructor.
    	
    class BlueBankRequest extends GenericRequest {
        public BlueBankRequest() {
    		super("Cheshire Cat Banking plc.");
        }
    }
    
    
  • The call to super must be the first thing in the Subclasses constructor.

Overriding

  • What happens if you call a method in the Subclass the same thing as a method in the Superclass?
  • If they have different arguments it doesn't matter - the JVM will call the one that works with them.
  • If the arguments are the same the Subclass will be used. The method is overridden.
  • We can also use super. to refer to the Superclass methods and variables in the same way as we use this. to refer to the present class.
  • If we declare the Superclass methods as final they can't be overridden by the Subclass. final Classes can't be inherited.

Abstract Classes

  • Classes that can only be inherited - can't make objects from them.
  • Include some methods and variables.
  • Any Class that extends it must provide all the missing methods, or be declared abstract itself.
	
abstract class GenericRequest {
    int pin = 0;
    int account = 0;
    void setAccNumber(int accountIn) {
		account = accountIn;
    }
    abstract int getPin () ;
}

	
class BlueBankRequest extends GenericRequest {
    int getPin () {
		return pin;
    }
}

  • BlueBankRequest objects have the setAccNumber method without defining it, but they must define getPin because all GenericRequest inheritors must have it.

Issues

  • If you have a class you'd like to pick up the methods from, just extend it: You don't actually have to have any content in the methods to get the compiler to see you've met your promises with abstract classes.
  • You can only extend one class or abstract class at a time. You can't, for example subclass both a menu item and a scrollbar - what would it look like?

Hierarchies

Finally, with both extension and implementation, we can build up flexible modelling and data-holding structures, where elements of behaviour can be replaced, and classes used to build up multiple subclasses.
  • We see this, for example in ArcGIS Geodatabases:

    route66 instantiates Freeway which inherits Road which inherits Line.

    ...each has additional variables suitable for holding, e.g. road speed limits.
  • But also in models:

    dale instantiates Human which inherits Primate which inherits Mammal with inherits Animal which inherits Agent.

    ...each has additional and overriding behaviours.

Review

  • If you have a class you'd like to pick up the methods from, just extend it:
    	
    class BlueBankRequest extends GenericRequest {
    
    
  • From then on, you get all the class' code for free!
  • If the class is abstract, the compiler will force you to put in the abstract methods.