OOPortal OOPortal

Inheritance/Polymorphism  «Prev 

Public Inheritance in C++

Public inheritance creates a class hierarchy in which a derived class object is in the form of an abstract base class object.
This is called "IS-A" inheritance; it is also referred to as interface inheritance. There exists an "IS-A" relationship between a class and the interface that it implements.

interface Maintainable{
	abstract void clean();
public class House implements Maintainable{
	static int counter = 0;
	public void clean() {
		System.out.println("Cleaning House: counter = " + counter);		
	public static void main(String[] args) {
		House h1 = new House();
		Maintainable m1 = new House();
		boolean state = m1 instanceof House;
		System.out.println("state = " + state);
Program output 
Cleaning House: counter = 1
Cleaning House: counter = 2
state = true

The instanceof operator says that m1 of type 'Maintainable' is a 'House' because the value of the variable state is true.
Class hierarchies should be about interface inheritance. In the classic example, a parent class Shape describes the properties and behaviors of all shape types using virtual and pure virtual member functions. The derived classes like circle implement the specifics.
The Circle is a Shape.