Logout

Home OOP Option Last Next

D.2.2

Define the term inheritance.

 

Teaching Note:

A parent object holds common data and actions for a group of related child objects. Multiple inheritance is not required.

 

Sample Questions:

----

Q1. Different modules in a (specific) program (provided in the exam paper) each open up a graphical user interface (GUI). Each GUI has a similar design, but contains differences specific to each module.

By giving two examples (from this specific program), explain how the principles of inheritance can be incorporated into the design of this administration program.

----

Q2. Separate OriginAddress and DestinationAddress classes will be created. The destination address may contain special instructions to the delivery person. The origin address contains a variable that indicates if the parcel was collected from the customer’s house or from the local post office.

Outline how these two new classes can be created with minimal duplication of code.

(The idea with a question like this is you would be expected to see that inheritance should be used. Having stated that, you could then come up with a good example of a shared attribute, like isLocalAddress, and one example each for particular attributes, like isReturnToSenderAddressAlso for the OriginAddress class, and contactPerson for the DestinationAddress class.)

----

Q3. The Department of Transport wants to expand this program to include similar analysis for bus and airplane journeys.

Describe how inheritance could be used to provide an organized set of classes for all of these forms of transport.

----

sdfsdfsf

JSR Notes:

OOP Super Power # 2: Inheritance

"I had no father. My mother sculpted me from clay and I was brought to life by Zeus." - Diana Prince, Wonder Woman (If Zeus were a Java class, he'd be Object.)


OOP Definition of inheritance
:

Inheritance is the OOP programming feature where one class can be extended to other classes. Those other classes inherit all of the super class' attributes, along with the actions which act on those attributes. Each sub-class then goes on to extend the functionality of the super-class, in its own way, by adding particular attributes of its own, along with the actions that act upon them.

In OOP terms, we call this relationship an "is a" relationship. The child (sub) class "is a" version of its parent (super) class, with a few additional features.

Do note it is usually the case that there are many sub-classes for one parent "super-class". And with Java, at the top of all inheritance hierarchies is always the class Object. Object is said to be the mother of all classes. All classes extend Object, though it's not necessary to write "extends Object" in the header. Object only has a few attributes and a handful of methods. Not many of them are often useful to us, but toString( ) we often will want to use.

General Examples of Inheritance Relationships

We'll start with a couple of general examples. And with them do note that inheritance is great in that it models real world relationships. Note also the interchangeability of the terms "parent" with "super-class, and "child" with "sub-class".

Parent "super-class": Animal

Children "sub-classes":


Or an example with different levels of hierarchy:

Parent "super-class: ISB_IT_Equipment

Children "sub-classes" of ISB_IT_Equipment, each with sub-classes of their own


Inheritance Hierarchies and UML Diagrams of Them

Inheritance is much easier to understand and see when represented in hierarchical diagrams, as is typically displayed in UML diagrams. Here first is a diagram of a simple class inheritance hierarchy:

 

And here are the two inheritance hierarchies we had in our SuperHero game:

 

And this is part of the hierarchy of Java Swing classes used for GUI:

So the best place to look here (above) would be our good old friend JTextField. Every time we make a textField in our GUI, we are making an instance (or actually, it's Netbeans doing it behind the scenes in the generated code) of a class which extends a class called JTextComponent, which, in turn is a class which extends a class called JComponent (which actually happens to extend Container, which extends Component, which extends the mother of all classes, Object).

So everything that Object, and JComponent, and JTextComponent etc. can do, JTextField can do too, along with a number of other methods of its own. It, in turn, is used as the super class for JPassowordField.

 

 

Inheritance via Java

Here's a quick example of two classes only from the first diagram above, in which Vehicle is the super class, and Car extends vehicle (i.e. is one of its sub-classes):

Note the two new Java reserved words needed to implement inheritance:
extends - this keyword follows the class name of the subclass in the header line of the sub-class; it is used to state which class is its super class.

super - this keyword means the super class. So used alone, it makes a call to the super constructor, or used with dot notation, it gives a class access to the super class' public members.

So in the case of super being used for calling the superclass constructor, what follows in its parameter list needs to be the correct list and order of parameters of one of the constructors of the superclass.

(There are three other Java keywords and concepts that are a bit beyond the IB CS curriculum, but I'll include a bit on both just so you know if/when you encounter them; they are: abstract and implements / interface.

abstract - a class which is declared as abstract is one which itself is not intended to be made instances of. Never-the-less, in its hierarchy, it does supply both common attributes and methods to the classes below it.

And an abstract method (within an abstract class) is one which does not have any body; rather it is just the header of a method which must be implemented - via overriding - in any non-abstract classes below it in the same hierarchy.

interface - a class which is declared as an interface is simply a collection of normal and/or abstract methods. Any class in the same project which implements an interface must override all of the abstract methods of the interface. (The Java keyword implements is used in the header of a class which implements an interface - so in the same way a class can extend either an abstract or another normal class, a class can also implement an interface.)

A good example of an interface is the Java interface Comparable. Any class which implements Comparable must have a method .equals( ) which takes in two Objects and returns a boolean, like the String class does, for example.

The idea of both abstract classes/methods and interfaces is to provide, within a framework, two things:
- a common set of method names
- expectations for a common capability or feature that extending/implementing classes will have


public class Vehicle{ private int numWheels;
private String color;

public Vehicle(int numWheels, String color){
this.numWheels = numWheels;
this.color = color;
}

public int getNumWheels(){
return numWheels;
}

public String getColor(){
return color;
}

public void setNumWheels(int numWheels){
this.numWheels = numWheels;
}

public void setColor(String color){
this.color = color;
}
}

public class Car extends Vehicle{

private boolean isHatchback = false;

public Car(int numWheels, String color, boolean isHatchback){
super(numWheels, color); //so this is a call to the super constructor, in this case Vehicle
this.isHatchback = isHatchback;
}

//gets...

//sets...

}

Here is the IntelliJ project our absolutely fully complete inheritance example from class - the superhere game. And a couple of images of it to get an idea.

 

(And here is another good example of inheritance from further on in the curriculum, where we make "stacks" and "queues" extended from the LinkedList class.)

 

 

Full Inheritance Coding Example

(Adapted from a past paper question.)

public class Train{
    private Engine engine;
    private Carriage[] carriages;
private DiningCar[] diningCars;
private int numWagonCount; private double totalWeight; public Train(int numCarriages, int numDiningCars){ engine = new Engine(); carriages = new Carriages[numCarriages]; diningCars = new DiningCars[numDiningCars]; numWagonCount = numCarriages + numDiningCars; } public Carriage [] getCarriages(){ return carriages; } //plus other accessor methods //and sets and other modifiers } ... public class Wagon{ private int idNumber; private double weight; public Wagon(int ID, double weight){ mIDNumber = ID; mWeight = weight; // Weight is in kilograms } // Accessor methods public double getWeight() { return mWeight; } public int getID() { return mIDNumber; } // Other methods ... } public class Carriage extends Wagon{ private double numSeats; public Carriage(int ID, int numSeats){ super(ID, 12000); //Empty carriage weighs 12000 kilograms this.numSeats = numSeats; } // Accessor methods public double getNumSeats() { return numSeats; } ... // Other methods ... } public class DiningCar extends Wagon{ private boolean hasKitchen; private int numTables; public DiningCar(int ID, boolean hasKitchen, int numTables){ super(ID, 32000); // Empty dining car weighs 32000 kilograms this.hasKitchen = hasKitchen; this.numTables = numTables; } // Accessor methods public int getHasKitchen() { return hasKitchen; } public double getNumTables(){ // Code to be written } // Other methods ... } --------------------------------------------------------------------

 

In Summary, Back to the Assessment Statement:
Define the term inheritance.

The answer can be basically the Teaching Note above. So: Inheritance is an OOP relationship between classes in which "parent object holds common data and actions for a group of related child objects".

 

And if asked to apply this to a specific program given in the exam paper:

For example, ZooAnimal and WildAnimal share the animalName attribute from Animal, but ZooAnimal adds the attribute dateAcquiredByZoo, and WildAnimal adds the attribute dateLastSeen.