Logout

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 Question:

sdfsdfsf

JSR Notes:

 

OOP Definition of inheritance:

Inheritance is the OOP programming ability for one class to be extended to other classes, which inherit all of the super classes attributes and actions which act on those attributes. Each sub-class will then extend the functionality of the super class in its own way by adding particular attributes and actions which act on them.

We call this relationship a "is a" relationship. The child class "is a" version of its parent class, with a few more added features.

And do note that it is usually the case that there are many various child classes from one parent "super-class".

Here's a quick example:


public class Vehicle{

private int numWheels;
private String color;

public Car(int numWheels, String color, boolean isHatchback){
this.numWheels = numWheels;
this.color = color;
}

public int getNumWheels(){
return numWheels;
}

public String color(){
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);
this.isHatchback = isHatchback;
}

//gets...

//sets...

}

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

 

Here are a couple of other general examples:

Parent "super-class": ISPEmployee

Children "sub-classes":


Parent "super-class: ISP_ITDevice

Children "sub-classes":