Inheritance explained with real life example

Inheritance is one of the key properties of Object Oriented Programming. I will be writing a series of posts on this topic.

In this post, I will be explaining Inheritance taking a real life example.

Lets take the use case of doctor’s treating patients. If I have to model this use case, we define a class called “Doctor”. Doctor, will have properties like experience, name, specialization etc. The class will also have methods with one important method to treat patients. Below is definition of Doctor class.

public class Doctor {
private String name;
private String specialization;
....

public void treatPatient(){
// treating patients
}
// more methods to follow
....

We have defined the attributes and on method above. But what should be the implementation of treatPatient method be? Hold on.. Lets come back to this a bit later.

Specialized Doctors

Doctors treat patients, but its a generic statement. Patients go to specific doctors based on the ailment. That’s where we have specialized doctors.

An Orthopedic is-a Doctor and he specializes in treating patients with bone ailments.

A Pediatric is also a Doctor and specializes in treating kids.

An Opthalmologiest treats patients with eye related ailments.

Similarly, there are many more specialized streams of Doctor, each of them specialized in one specific stream.

To establish the is-a relationship in programming, we use the extends keyword. By just adding the extends keyword, a class can inherit all instance members from another class. The former is called the Child class or Derived class and the latter is called Base class or Parent class.

The whole point of a class extending other class is to add more specialization.

This brings in the relationship of is-a relationship which is also referred to as parent-child relationship.

There are quite a few interesting things to note here. Lets look at them, as they are key to understand and reinforce the Inheritance concepts.

  1. An Ortho is-a Doctor and inherit the attributes and methods from Doctor class.
  2. Doctor class does not even know that it is being extended by other class. In fact it does not even know the existence of Orthod/Padietric class. There is no reason for it to know.
  3. An Ortho can only inherit attributes and method from one class. In Java, a class can extend only one class. More on this a bit later…
  4. Both Ortho and Padietric are extending from Doctor class and share the is-a relationship with Doctor, but there is no relationship between them. Ortho is not a Padietric and vice versa.

Lets translate all the above points into the code.

Orthopedic Class:

public class Orthopedic extends Doctor {    //inherit instance attributes and methods from Doctor class
// more method to follow
...
}

Pediatric Class:

public class Pediatric extends Doctor {
// inherit instance attributes and methods from Doctor class
//more method to follow
...
}

The whole point of extending is to make the class a more specialized version of its parent class. That means, in addition to the attributes and methods inherited from base class, the subclass can also add its own methods.

Lets add more methods to both Orthopedic and Pediatric class

public class Orthopedic extends Doctor {
// specialized methods added
public void conductXRay(){}
public void conductCTScan(){}
}

Pediatric Class:

public class Pediatric extends Doctor{
//specialized method for Pediatric
public void examineKids(){}
}

Awesome!! So far we have defined Doctor class and established the parent child relationship with both Orthopedic and Pediatric classes.

Now, lets write the client program and assert the key points

public class Client {
public static void main(String args[]){
Doctor doctor = new Doctor(); //valid
Orthopedic ortho = new Orthopedic(); //valid
Doctor orthoDoc = new Orthopedic();// valid
Doctor peditricDoc = new Pediatric(); // valid
// not valid since peditaric is not a Orthopedic
Orthopedic orthoDoctor = new Pediatric();
// not valid since Doctor is not Orthopedic
Orthopedic doctor1 = new Doctor();
}
}

The valid ones in the above program are obvious. Let me explain the invalid ones.

Orthopedic orthoDoctor = new Pediatic();

We need to check if there is an is-a relationship. For the statement to be valid, the left hand operand should pass the is-a test. Here, since Pediatric is-not-a Orthopedic, hence the above statement is not a valid one.

Lets take the next statement

Orthopedic doctor1 = new Doctor();

Again, in the above statement, the left operand, which is Doctor, does not pass the is-a test becomes an invalid statement and throws compile time error. A Doctor is not an Orthopedic. Its the Orthopedic who is a Doctor.

“instanceof“ operator for rescue.

Use the “ instanceof ” keyword to check if there exists an is-a relationship between object and a class.

Doctor doctor = new Doctor();
Orthopedic ortho = new Orthopedic();
System.out.println(doctor instanceof Orthopedic); // prints false
System.out.println(ortho instanceof Orthopedic); //prints true
System.out.println(orthoDoc instanceof Pediatric); //prints false
System.out.println(ortho instanceof Doctor); //prints true

Multilevel Inheritance

We can have many levels of Inheritance. Lets take an example. A Knee Surgeon is a specialized version of Orthopedic who treats patients with knee related ailments.

A Knee Surgeon class extends Orthopedic class and inherit all the attributes and methods from Orthopedic. Hence Knee Surgeon is-a Orthopedic and also pass the is-as test on Doctor class.

public class KneeSurgeon extends Orthopedic {
public void kneeSurgery(){}
}

Examples of Inheritance in JDK

Inheritance is the core foundation of OOP’s concept. There are many classes in JDK which uses this powerful concept. Some of the examples are below

  1. ArrayList extends AbstractList.
  2. Wrapper classes like Integer, Double extends Number class.
  3. SimpleDateFormat extends DateFormat class.
  4. All classes in Java extends Object class implicitly.

In the next post, I will discuss advance topics on Inheritance. We will look into method Overriding and type casting.