Understanding OOP in Java (Inheritance)

The Javatar
3 min readJan 2, 2021

Inheritance is one of the pillars of Object Oriented Programming in any programming language. It is just as the name implies, a ‘child’ class getting all of it’s features from a ‘parent’ class.

In inheritance, we have the parent, base or super class, which is the class whose features are being inherited, and there’s the child, derived, extended or sub class, which is the class inheriting features from the parent class.

Inheritance is the main mechanism behind code reusability. The extends keyword is used to implement Inheritance.

  • A class can inherit from another class using the extends keyword but inherits from an interface using the implements keyword
  • An interface can inherit from another interface using the extends keyword
  • A class can only extend a single class but can implement multiple interfaces
  • An interface can extend multiple interfaces
  • Private members and constructors of the super class are not inherited

The syntax for Inheritance in Java :

class Subclass extends Superclass {
//type your code here
}

An example of inheritance

class Vehicle {
final int max_speed = 50;
}
public class Truck extends Vehicle {
int no_tyres = 4;
String brand_name = "Cybertruck";
public static void main (String [] args){
Truck tr = new Truck();
System.out.println(tr.brand_name+" has "+tr.no_tyres+" tyres and max speed of "+tr.max_speed);
}
}

This outputs

>> Cybertruck has 4 tyres and max speed of 50

From the example above, the class Truck extends the Vehicle and inherited the field max_speed and is accessed like it is defined in the Truck class. Note that private fields or methods and constructors of the parent class are not inherited.

Types of Inheritance

There are five(5) types of inheritance but Java supports only three(3)

  • Single Inheritance

In single inheritance, one child class inherits directly from a parent class e.g

class Vehicle{
public void drive(){
System.out.println("Driving...");
}
}
class Car extends Vehicle{
public void left(){
System.out.println("Turning left...");
}
}
public class Single_Inheritance{
public static void main(String[] args){
Car honda = new Car();
honda.drive();
honda.left();
}
}

This outputs

>>Driving...
>>Turning left...
  • Multilevel Inheritance

In multilevel inheritance, there is a chain of single inheritance i.e class B extends class A, class C extends class B (who already extends class A) e.g

class Vehicle{
public void drive(){
System.out.println("Driving...");
}
}
class Car extends Vehicle{
public void left(){
System.out.println("Turning left...");
}
}
class raceCar extends Car{
public void right(){
System.out.println("Turning right...");
}
}
public class Single_Inheritance{
public static void main(String[] args){
raceCar honda = new raceCar();
honda.drive();
honda.left();
honda.right();
}
}

This outputs

>>Driving...
>>Turning left...
>>Turning right...
  • Hierarchical Inheritance

In hierarchical inheritance, two or more classes inherit a single parent class. e.g

class Vehicle{
public void drive(){
System.out.println("Driving...");
}
}
class Car extends Vehicle{
public void left(){
System.out.println("Turning left...");
}
}
class Bus extends Vehicle{
public void right(){
System.out.println("Turning right...");
}
}
public class Single_Inheritance{
public static void main(String[] args){
Car honda = new Car();
Bus hiace = new Bus();
honda.drive();
honda.left();
hiace.drive();
hiace.right();
}
}

This outputs

>>Driving...
>>Turning left...
>>Driving...
>>Turning right...
Image from javatpoint.com
Image from https://www.javatpoint.com/inheritance-in-java#:~:text=Inheritance%20in%20Java%20is%20a,behaviors%20of%20a%20parent%20object.&text=The%20idea%20behind%20inheritance%20in,fields%20of%20the%20parent%20class.
  • Multiple Inheritance and Hybrid Inheritance

In multiple inheritance, a class inherits from more than one parent class while in hybrid inheritance ,there is a combination of single inheritance and multiple inheritance.

Image from https://www.javatpoint.com/inheritance-in-java#:~:text=Inheritance%20in%20Java%20is%20a,behaviors%20of%20a%20parent%20object.&text=The%20idea%20behind%20inheritance%20in,fields%20of%20the%20parent%20class.

Java does not support these two types of inheritance because of what is called the diamond problem. Consider the image above where class C inherits from class A and class B, if A and B both have a method called foo() but does different stuff, if class C calls the method foo(), which class does it call the foo() method from? A or B? Due to this ambiguity, Java doesn’t support multiple and hybrid inheritance and returns a compile-time error if implemented.

To learn more, visit;

--

--

The Javatar

An adventure to becoming a fully realized Javatar 😂😎