OOPs: Inheritance and Polymorphism

Mukesh Chaudhary
5 min readOct 9, 2020

--

Let’s talk deep inside inheritance and polymorphism together

In this blog, I am going to write little bit depth inside most important concept of Object-Oriented Programming called inheritance . You may find article every where on this topic. But I found something missing together. It means many article only defines inheritance and examples , they don’t explain how to implement in real life projects. Even, in inheritance , other concept also come with . It’s called polymorphism. So I say that if you want to understand completely about inheritance than we have to understand polymorphism concept together. That give us better clarity on how to implement on projects .I also try to describe when we need to inherit and why. Here , I take all examples in java programming language because I think it gives better clear example to understand. Let’s start definition:

Definition:

  1. Inheritance : It is a mechanism in which one object acquires all the properties and behaviors of a parent object. It is an important part of OOPs (Object Oriented programming system).
  • subclass (child) — the class that inherits from another class. It’s also called a derived class, extended class, or child class.
  • superclass (parent) — the class being inherited from.It is also called a base class or a parent class.

Example:

class Animal {
public void animalSound() {
System.out.println("The animal makes a sound");
}
}

class Pig extends Animal {
public void animalSound() {
System.out.println("The pig says: wee wee");
}
}

class Dog extends Animal {
public void animalSound() {
System.out.println("The dog says: bow wow");
}
}

1.1. Why use inheritance in java

  • For Method Overriding (so runtime polymorphism can be achieved).
  • For Code Reusability.

1.2. Types of inheritance in java:

a. Single Inheritance

b. Multilevel Inheritance

c. Hierarchical Inheritance

d. Multiple Inheritance

e. Hybrid Inheritance

Pictures of all types give more clarity . These are

2. Polymorphism:

Polymorphism is the ability for some code structures in an OOP language to be treated as different structures at runtime.

2.1 Object Polymorphism:

When discussing inheritance, it was explained that when class B inherits from class A, an object of class B is also considered to be an object of class A. That’s object polymorphism.

2.2 Method Polymorphism

In most OOP languages, methods are differentiated by one another by their signature, which is a combination of the method’s name, and the types, number and order of parameters that are passed in. This system of differentiation allows us to override and overload methods.

2.3 Type of Polymorphism

2.3.1 Compile-time polymorphism

2.3.2 Run-time Polymorphism

Above example , public void animalSound() is run-time polymorphism. Method overloading is called compile-time polymorphism, because these methods are differentiated by the compiler based on the parameters they are given.

Implementation:

All above is definition of inheritance and polymorphism . We can find it any where, and we should know too. But , here, I want to discuss implementing of these concept in project level. I also want to say about relationship between two or more class while one class inherits another class. Actually , Inheritance means it established relationship between them. There are two type of relationship . Inheritance present “IS-A” relationship and another is composition relationship. Let see one example before go to further.

class Animal {
public void animalSound() {
System.out.println("The animal makes a sound");
}
}
class Vehicle extends Animal {
public void vehicleSound() {
System.out.println("Vehicle Sound !!");
}
}

In above example, we have two class Animal and Vehicle where Vehicle class inherits Animal class. Is it create any logical meaning by inheritance even though there is no error technically. Obviously , There is no meaning. So we should always inherits proper way from which we can establish proper relationship. Let’s another example

class Animal {
public void animalSound() {
System.out.println("The animal makes a sound");
}
}
class Dog extends Animal {
public void animalSound() {
System.out.println("Dog Sound !!");
}
}

Now Dog class inherits animal class . Then object of dog class also inherits all properties and behavior of animal class which also give logical meaning. And this is called “ IS-A” relationship. But When we create object of any class on another class , then relationship between created object and class is called “ HAS-A” ( composition) .

Example:

class Dog {
public void animalSound() {
System.out.println("Dog sound");
}
}
class Person {
public static void main(String[] args) {
Dog dogObj = new Dog(); // "HAS-A" relationship dogObj.animalSound();
}
}

Important points in java :

Here , I used all examples in java programming language . So we have to remember some important points in java during inheritance concept.

  • Java doesn’t allow to multiple and hybrid inheritance in classes like C++ because Java become confuse to override method (run -time Polymorphism) while same method name appear in both classes. Example
class Animal {
public void animalSound() {
System.out.println("The animal makes a sound");
}
}
class Mammal {
public void animalSound() {
System.out.println("The mammal makes a sound");
}
}// error , multiple class inheritanceclass Dog extends Animal,Mammal {
public void animalSound() {
System.out.println("Dog Sound !!");
}
}
  • But Java can inherit multiple Interfaces.

Conclusion:

Inheritance and polymorphism are very important concepts which are used in every single steps in projects. We need to inherit other user defined class or built-in class to save time and proper manage code. Without these concepts , we can’t do any thing object-oriented programming language projects. And I also focused on implementation, relationship topic than definition. We should able to distinguish classes , objects, entities , behaviors , properties and relationship between them during gathering requirements from users. Then , We can organize proper way coding and projects.

References:

https://www.javatpoint.com/java-tutorialhttps://www.geeksforgeeks.orghttps://www.w3schools.com/javahttps://beginnersbook.com/2013/03/inheritance-in-java/https://www.scientecheasy.com/2020/05/java-abstraction.html/https://books.google.com/books?id=AokcsKn-1iIC&q=ANSI&dq=related:OXFORD600007604&lr=&source=gbs_word_cloud_r#v=snippet&q=ANSI&f=false

--

--