Comparison between Inheritance and Polymorphism

 

Java is object-oriented programming language. There are 4 characteristics of object-oriented languages which are: Polymorphism, Inheritance, Encapsulation, Abstraction.

Polymorphism-Polymorphism refers to the ability of OOPs programming languages to differentiate between entities with the same name efficiently.

Inheritance-Inheritance is an important pillar of OOP (Object Oriented Programming). It is the mechanism in java by which one class is allowed to inherit the features (fields and methods) of another class.

Encapsulation-Encapsulation is defined as the wrapping up of data under a single unit. It is the process of keeping classes private so that they cannot be changed by external code.

Abstraction-Data Abstraction is the property by virtue of which only the essential details are displayed to the user.


Difference Between Polymorphism and Inheritance

















Polymorphism occurs when there are two or more distinctly different traits in the same population of species, or the development of forms or morphs, whereas inheritance is the process in which the properties of a parent are passed on to the offspring, even if such properties have not been clearly defined by other means.

Inheritance refers to the use of structure and behaviour of a superclass in a subclass. In essence, inheritance entails creating a class and then introducing new classes into your program that inherits properties from an existing base class. Polymorphism is the ability to make the same code act differently depending on the type of object it is working with. Polymorphism may or may not be directly related to concepts as diverse as inheritance, biodiversity, and adaptation. The difference between polymorphism and inheritance in OOP is that polymorphism is a common interface for multiple modules, and inheritance is about creating a new class using the attributes and methods of an existing class. The main key difference between polymorphism and inheritance in object-oriented language is that polymorphism is the ability of an object to behave in different ways, while inheritance is the creation of a new class using the properties and methods of an existing class. The main difference between inheritance and polymorphism in Java is that inheritance allows a class to access the properties and methods of an already existing class, whereas polymorphism allows an object to behave in many ways.

Inheritance allows code reuse, while polymorphism is the appearance of a function with a different form. Inheritance is implemented for classes and polymorphism is for methods / functions. Inheritance allows the programmer to create class hierarchies from one class that inherits from another class by setting its attributes and behaviour, while polymorphism allows the user to create different methods for different requirements and ensure that the correct method is executed based on a call to a gender object. Polymorphism is a part of inheritance and could not exist without it. If you don't override it, this is not polymorphism, it is just inheritance.

Moreover, inheritance is implemented at the class level, while polymorphism is implemented at the method level. In addition, while inheritance provides code reuse, polymorphism allows methods to be called appropriately at compile time and run time. Speaking of lines of code in a program, inheritance reduces the number of lines of code because the child class reuses the code of the parent class by inheriting it, while in polymorphism the total program length increases as one method is implemented in different pathways.


The Different types of Inheritance



  • Single Inheritance

         Creating derived based class (subclass) from a single base class is called single inheritance. 

  • Multiple Inheritance in Java

         Creating a derived class from numerous base classes is called as Multiple Inheritance. In object-oriented programming (OOP), multiple inheritances are available in C++, but not in Java.

  • Multi-Level Inheritance in Java

         In Multi-Level Inheritance in Java, a class extends to another class which has already been extended from another class. For example, a class X that extends class Y and class Y extends from another class Z, then this scenario is said to follow Multi-level Inheritance.

  • Hierarchical Inheritance in Java

         Multiple Child class inheriting a single class or vice versa is called as Hierarchical Inheritance. Basically, more than one child class extends a single parent class, or a single parent class has more than one child class.

  • Hybrid Inheritance in Java

        The combination of Inheritance is called as Hybrid Inheritance. It has more than one kind of inheritance. For example, if we have class X and class Y that extend class Z and then there is another class K that extends class X, then this type of Inheritance is known as Hybrid Inheritance.


Types of Polymorphism




  • Compile Time Polymorphism

         Polymorphism that is resolved during compiler time is known as Compile time polymorphism. Method overloading which allows to have more than one method having the same name, if the parameters of methods are different in number, sequence and data types of parameters is an example of Compile Time also called as Static Polymorphism.

  • Runtime Polymorphism

         It is commonly known as Dynamic Method Dispatch. Resolving the call of an overridden method at runtime is called as runtime polymorphism. Here a reference variable of superclass is called through an overridden method.


Benefits of Inheritance

Benefits of Inheritance

Inheritance helps in code reuse. The child class may use the code defined in the parent class without re-writing it. Inheritance can save time and effort as the main code need not be written again. Inheritance provides a clear model structure which is easy to understand. An inheritance leads to less development and maintenance costs. With inheritance, we will be able to override the methods of the base class so that the meaningful implementation of the base class method can be designed in the derived class. An inheritance leads to less development and maintenance costs. In inheritance base class can decide to keep some data private so that it cannot be altered by the derived class.

One of the main benefits of inheritance is to minimize the amount of identical code in an application by sharing common code within several sub classes. As the equivalent code exists in two related classes, the hierarchy can usually be rewritten to move the common code up to a mutual superclass. This also tends to result in a better organization of code and smaller, simpler compilation units. Because classes that inherit from a common superclass can be used interchangeably, inheritance can make application code more flexible to update. If the return type of a method is superclass then we have

  • Reusability – the ability to use base public class method without rewriting the same.
  • Extensibility – to extend the base class logic required by the business logic of the derived class.
  • Data hiding – to avoid the data getting change by the derived class the base class choose some data to keep it as private.
  • Overriding –to get the meaningful implementation of the base class method a derived class can be designed for that we need to override the methods of base class.


Benefits of Polymorphism

In other dynamic languages, polymorphism can be achieved through Duck Typing, which means that classes do not even need to share the same base class or interface, they only need a method with the same name. Or even more like JavaScript, you don't even need a class, only objects with the same method name can be used in a polymorphic way. Overloading allows you to use methods in the same class or subclass with the same name, but different parameters and it may be overloading as well as overriding.

Polymorphism gives the liberty to programmers to reuse, evaluate and execute the program, modules, forms written once. In few aspects, it could be repeated.

 We use the odd variable name for storing different types of variables like Int, Float, etc.

Polymorphism helps in reducing the pairing of multiple functionalities.

Method overloading can be extended to builders which allows multiple ways of initializing class objects which also helps to identify several builders for managing various forms of initialisations. Method overriding functions, as well as inheritance without the requirement for recompilation, enabling existing groups' code to be reused.


If you enjoyed this article, share it with your friends and do let us know your thoughts in comment section!

OOP HOME ASSIGNMENT BLOG

BATCH-1 , ICA

MEMBERS:

01. Shrutika Abhang

04. Hunar Agrawal

06. Anirudha Sonawane

16. Vedant Bhoir


Comments

  1. This comment has been removed by the author.

    ReplyDelete
  2. Keep Growing up Vedant with such informative & interesting 😌

    ReplyDelete
  3. Very informative πŸ‘πŸ»

    ReplyDelete
  4. Very informative well done guyss

    ReplyDelete
  5. Very informative blog ...Really got some valuable info ...

    ReplyDelete
  6. Thanks for such a valuable contributionπŸ‘

    ReplyDelete
  7. Amazing blog , great work by team)

    ReplyDelete
  8. It was really informative and the information easy to digest as well, would share it with other people as well.

    ReplyDelete
  9. Thank you for this valuable information

    ReplyDelete
  10. Great work πŸ”₯πŸ”₯πŸ”₯

    ReplyDelete
  11. Well explained and sorted blog with appropriate and required concepts related to the topic.

    ReplyDelete
  12. Very Informative and Helpful! Nice Work.

    ReplyDelete

Post a Comment