What Is Inheritance in Object Orient Programming? How to Make

Asiri Gunasena

Published:

Object Oriented Programming

Inheritance in Object Orient Programming

Inheritance in Object Orient Programming is one of the well-known methods of object-oriented programing. Inheritance in Object Orient Programming is acquiring all the behaviors and attributes from one another. This means one class’s properties are derived from another class.

In this article hope to explain what is the inheritance in Java, java inheritance example, multiple inheritance Java, advantages of Inheritance in Object Orient Programming and etc.

To complete inheritance successfully, there should be a minimum of two classes. Those are called Superclass and Subclass. The subclass class is known as the child class and inherits property from Superclass. Superclass is also known as Parent class or base class.

After creating an inheritance there is create a relationship between them. It’s called IS A relationship. As an example, when inheriting BMW from the basic car there is a relationship between BMW IS A Car. Likewise Physician IS A Doctor.

Inheritance Real Life Example

Example 1

Let’s move on to a real-life example of inheritance. My first example is dogs and birds are inherited from animals. When it’s come to this example animals have common attitudes and behaviors including eating and sleeping. 

 Inheritance In Object-Oriented Programming
Inheritance of dogs and birds from Animals

Those common attitudes and behaviors are inherited by dogs and birds. But dogs have special types of attitudes and behaviors such as barking and sensing smell. Also, birds are able to fly. Consequently, dogs and birds have all the behaviors that animals have. Dogs are able to Bark, eat, sleep, and have a smell sense ability likewise birds fly, eat and sleep.

Example 2

Let’s take a second example. Vehicles are built by adding seats and moving ability. Consider Bicycles and Cars inherited from Vehicle. But bicycles are pedaling although cars have an engine. As a result of inheritance, bicycles are able to move by pedaling and have a seat. In the same way, cars are able to move using an engine and have seats. Those Moving abilities and seats are inherited from vehicles.

Those examples illustrate Inheritance everywhere. Most of the objects in the world derive from some base object and IS-A relationship between them.

Inheritance Real Life Example with Java

In java Use the keyword “Extend” to inherit Superclass to subclass. The “Super” keyword is used to call the parent class properties or attributes. But Multiple Inheritance does not support in java thorough the classes.

Diamond Problem in Java

Diamond Problem
Diamond Problem

The below example gives a compilation error because of extends multiple parent classes to a single subclass. Because multiple inheritances are not allowed in java. Because of code raise diamond problem through the Java compiler.

// Create parent with myMethod
class Parent1{
   public void myMethod(){
      System.out.println("Parent 1 Method");
   }
}

// myMethod derived through the inheritance to child1 
class Child1 extends Parent1{
}

// myMethod derived through the inheritance to child2
class Child2 extends Parent1{
}

// Trying to inherit Child1 and Child2 . Then this get Compilation error
public class DiamondInInheritance extends Child1,Child2{
   public static void main(String[] args) {
   }
}
Diamond Problem in Java Compilation Error

Multiple inheritances through interfaces in Java

But if you really need multiple inheritances that’s possible to achieve through interfaces. In java possible to implement multiple interfaces. Interfaces do not extend with class, but you must implement those. See the below example for Interfaces implemented in java.

// Abstract Behaviour for Run
interface Runnable{
   public void run();
}

// Abstract Behaviour for Stop
interface Stoppable{
   public void stop();
}

// Implements two interfaces by using "implements" keyword
class MyCar implements Runnable,Stoppable{
   //Implements run Method here
   public void run(){
      System.out.println("My car start to Run");
   }
   //Implements stop Method here
   public void stop(){
      System.out.println("My car going to Stop");
   }
}

Also, keep in mind if a parent is an Interface and a child’s also an Interface then have to use the “extend” keyword to make the relationship between them. See the below example for Interface extend with Interface in Java.

// Abstract Behaviour for method1
interface Interface1{
   public void method1();
}

// Abstract Behaviour for method2
interface Interface2 extends Interface1{
   public void stop();
}

Below is the complete example for the java Inheritance. Here the above Animal example demonstrates the code for acquiring all the behaviors and attributes through inheritance.

// Base Class for Animal (Parent Class) , 
// added Common behaviours for animal as sleep and eat, 
// Also added common attributes as birthday and everage life period
class Animal{
   protected int everageLife;
   protected String birthDaye;
   public void sleep(){
      System.out.println("Animal Sleeeping");
   }
   public void eat(){
      System.out.println("Animal Eating");
   }
}

// Sub Class for Dog extends animal , added Dog spesific behaviours
class Dog extends Animal{
   public void bark(){
      System.out.println("Dog Barking");
   }
   public void smellSence(){
      System.out.println("Dog Sensing smell");
   }
   public String toString(){
     return "Dog everage life is " + everageLife+ " years and My dog born on " + birthDaye;
   }
}

// Sub Class for Bird extends animal , added Bird spesific behaviours
class Bird extends Animal{
   public void fly(){
      System.out.println("Bird Flying");
   }
   public String toString(){
     return "Dog everage life is " + everageLife+ " years and My dog born on " + birthDaye;
   }
}

public class TestInheritance {
   public static void main(String[] args) {
      // Dog derived Animals things and able to change inside the dog object
      Dog dog = new Dog();
      dog.everageLife=10;
      dog.birthDaye="2021.08.18";
      dog.bark();
      dog.eat();
      System.out.println(""+dog);
      System.out.println("");
      // Bird derived Animals things and able to change inside the dog object
      Bird bird = new Bird();
      bird.everageLife=1;
      bird.birthDaye="2021.09.18";
      bird.fly();
      bird.eat();
      System.out.println(""+bird);
   }
}

Output

Dog Barking
Animal Eating
Dog everage life is 10 years and My dog born on 2021.08.18

Bird Flying
Animal Eating
Dog everage life is 1 years and My dog born on 2021.09.18

Types of Inheritance

There are main five types of inheritance. If a Programming language supports object-oriented programing but supporting all the inheritance types is up to the language. As an example, java does not exactly support multiple inheritances.

Single Inheritance

One subclass is derived from one superclass.

Multiple Inheritance

One Subclass derived from multiple Superclasses

Multilevel Inheritance

One subclass derived from the derived superclass

Hierarchical Inheritance

More subclasses deride from one Superclass

Hybrid Inheritance

More than one inheritance type is used to derive classes

Advantages of Inheritance in Object Orient Programming

  • Code Reusability: Code can be reused After deriving the parent class to the child
  • Maintainability: If a change in the parent class changes will apply to all sub-classes.
  • Minimize the Code duplication by redundant code moving to the parent.
  • Improve the code standard and clear code every time easy to understand

Summary

Inheritance in Object Orient Programming is acquiring all the behavior attributes from another. Inheritance is used differently by programming languages according to the language definition, but the final achievement looks the same behind the scene. The usability of Inheritance in the real world is everywhere. Also, it gives the above advantages to making programming clear and easy.

ennicode

Address: 89/1 Rabbegamuwa, Handessa, Kandy, Central, Sri Lanka

Email to: Primary: [email protected]

Services

E-Learning

Company Websites

Support and Configuration work

Banners, Covers, and Post

Web Development & Configurations

Content Writing and Marketing

Contact

Ennicode