Comparison: Inheritance & Polymorphism

 


Comparison Inheritance & Polymorphism



Inheritance is the mechanism by which an object inherits all the properties and behaviors of its parent object. This is an important part of OOP (Object Oriented Programming System).

 


 Terminology used in inheritance 

 Class: A class is a group of objects that share common properties. This is the template or blueprint from which the object was created. 

 Subclass / Subclass: A subclass is a class that inherits from another class. It is also called a derived class or child class.  

Parent Class: It is the class from where the features are inherited by subclass. also  called a base class/parent class. 

 Reusability: As the name seggests, reusability is a mechanism which facilitates you to reuse the fields and methods of the existing class when you create a new class.You can use the same fields and methods as defined in the previous class 

syntax. class subclass name extends superclass name

class Subclass-name extends Superclass-name  

{  

   //methods and fields  

}  


 

Types of inheritance in java

Based on the  class, Java has three types of inheritance: simple, hierarchical and multi-level.  In Java programming, multiple  hybrid inheritances are only supported through the interface. 



Polymorphism


Java polymorphism is a concept that allows you to perform a single action in different ways Polymorphism comes from the two Greek words poly and morphs.. The word "poly"-(lot), and "morph"-(form). Therefore, polymorphism means many forms.







Example of polymorphism




Types of polymorphism:


Java has two types of polymorphism: compile-time polymorphism and run-time polymorphism. Method overloading and method overriding allow you to perform polymorphism in Java. If you are overloading static methods in Java, this is an example of compile-time polymorphism. Here we will discuss Java runtime polymorphism  

 

 

 Runtime polymorphism: 

 Runtime polymorphism, or dynamic method dispatching, is the process by which  overridden method calls are resolved at run time rather than at compile time. 

  Run-time polymorphism determines the behavior of a method  at run time. 

 Therefore, at run time, the JVM (Java Virtual Machine) binds the method call to the method definition / body and at run time calls the relevant method  when the method is called.   This happens because the object is created at run time and the method is called through the object of the class. The Java compiler has no awareness of the method to be called on an instance during compilation. Therefore, JVM invokes the relevant method during runtime.  Runtime polymorphism can be implemented / implemented in Java using method overrides. Method overriding is a mechanism that overrides a base class method  in a derived class to provide a more specific implementation.  The method signatures of the base class and the derived class are the same, but only the implementation is different. 

 

 Compile time polymorphism: 

 The polymorphism that appears during compilation is called static polymorphism in Java. In static polymorphism, the behavior of a method is determined at compile time.Therefore, the Java compiler binds method calls to the method definition / body during compilation.  Therefore, this type of polymorphism in Java is also known as compile-time polymorphism. 

 

 

 

 

 

Example:

Class animals { 

 public void animalSound () { 

 System.out.println ("Animal makes noise"); 

 } 

 } 

 

 Class pigs extend animals { 

 public void animalSound () { 

 System.out.println ("What the pig says: wee wee"); 

 } 

 } 

 

 Dogs in class extend animals { 

 public void animalSound () { 

 System.out.println ("What the dog says: Wow Wow");

 

  }

}


 

 

difference between Inheritance & Polymorphism

 

INHERITANCE

POLYMORPHISM

1.

Inheritance is one in which a new class is created (derived class) that inherits the features from the already existing class(Base class). 

Whereas polymorphism is that which can be defined in multiple forms.

2.

It is basically applied to classes.

Whereas it is basically applied to functions or methods.

3.

Inheritance supports the concept of reusability and reduces code length in object-oriented programming.

Polymorphism allows objects to determine the format of functions to implement at compile time (overload) and run time (override).

4.

Inheritance can be simple, hybrid, multiple, hierarchical, and multilevel. 

It can be either compile-time polymorphism (overload) or run-time polymorphism (override). 

5.

Used for pattern design.

It  is also used for pattern design.

 


Conclusion


It is no exaggeration to say that both polymorphism and inheritance are important concepts in implementing a program. Both are the basis of the idea of ​​object-oriented programming. There are many differences between polymorphism and inheritance because they have two very different purposes. 

 One thing to keep in mind when writing code is that you want to refactor the code you've already written (basically defining a class and reusing it in your code to serve similar or different purposes. increase). You must use inheritance. Consider using polymorphism if you want to reduce the overall confusion of your code and use the same function names for similar tasks.


 Thank you for reading .....





Published by:

  1. Pratiksha Bandole 

  2. Pritam Bhatkar 

  3. Abhishek Degil

  4. Yash Ganbote 

  5. Vedika Gosavi 


Chemical department | Vishwakarma Institute Of Technology, Pune |

Comments

  1. Good content written but
    More deep content can be added.

    ReplyDelete
  2. Great stuff guys. Real important and instructive stuff. Noice job

    ReplyDelete
  3. Important information.....

    ReplyDelete
  4. Very informative 📜.. nice work guys 👍🏻

    ReplyDelete
  5. Very informative blog. Each contents are really good

    ReplyDelete
  6. Amazing write-up Team! The way you cover this topic in a single blog with excellent content is really appreciated. Keep it up.

    ReplyDelete
  7. Very Informative blog

    ReplyDelete
  8. Good Works Guys .... Keep it up

    ReplyDelete
  9. Good one 👍🏼

    ReplyDelete
  10. Informative Blog

    ReplyDelete
  11. Keep it up Guy's

    ReplyDelete
  12. 👍🏼👍🏼

    ReplyDelete
  13. Thanks for sharing such a great information.. It really helpful to me..I always search to read the quality content and finally i found this in you post. keep it up! Guys

    ReplyDelete
  14. Impressive Blog keep it up Guy's

    ReplyDelete
  15. Enjoyed reading the blog above , really explains everything in detail, the blog is very interesting and effective. Thank you and good luck in the upcoming blog.

    ReplyDelete
  16. Very nice information 👍👍

    ReplyDelete
  17. Very nice and informative

    ReplyDelete
  18. The way you cover this topic in a single blog with excellent content is really appreciated. Want more such blogs !!!
    Excellent work guys!!

    ReplyDelete
  19. You guys done the great job Overall each information get covered in a blog and it’s quite informative for me!😊Keep it up guys!!

    ReplyDelete
  20. This comment has been removed by the author.

    ReplyDelete
  21. Amazing helpful, enlightening and informative this blog is. I would suggest all students interested in this field to read this blog. Had fun reading this one. Great work guys

    ReplyDelete
  22. Great content guys👍🏽. Found this when i was surfing over the net for 3D materials and found this blog very informative and useful. Loved how the past , present and he future of hybrid algorithm method was mentioned in detail. I enjoyed reading this blog and this is beneficial for students etc. You guys have my gratitude for this great stuff.

    ReplyDelete

Post a Comment