Inheritance vs Polymorphism

 

Comparison Of Inheritance And Polymorphism 


            As we all know that Java is one in all the foremost popular programming languages worldwide. it absolutely was created by James Gosling and Patrick Naughton, employees of Sun Microsystems, with support from Bill Joy, co-founder of Sun Microsystems. Java is understood for being a multi-purpose language, secure and Object-oriented programming (OOP) language.

We can construct applications for a spread of devices using this programing language. it's a broad range of applications, allowing us to develop software for mobile devices, point-of-sale terminals, ATMs, IoT (Internet of Things), and web content.

OOPS is that the backbone of Java because it's an object-oriented programing language. Java structures a program around well-defined objects and interfaces. In OOPS, there are four pillars, which are listed below. These ideas are aimed toward incorporating real-world entities into computer applications.


• Abstraction

• Encapsulation

• Inheritance

• Polymorphism

       Image Source -  https://images.app.goo.gl/VSc6ZU6xXKCCKNgM9

As there are four pillars of OOPS and this blog is predicated on comparison between Inheritance and Polymorphism, we are going to see how they're different from one another and what properties they contains,

Inheritance

            The term 'Inheritance' comes from the word "Inherit" which implies, "to derive any quality, features or characteristics from family".

"Inheritance is defined as a mechanism where the sub or child class inherits the properties and characteristics of the super class or other derived classes. It also supports additional features of extracting properties from the child class and using it into other derived classes."

Inheritance is mechanism within which a brand new class inherits the properties of the already exits class. Its supports the concept of code reusability and reduces the length of the code in object oriented programming language. Inheritance is one in all the cornerstones of object oriented programming because it allows the creation of hierarchical classification. you'll use inheritance to style a broad class that defines traits that are shared by a group of related things. This class is inherited by other, more specific classes, with every one providing it’s own features. within the terminology of Java, inherited class is termed a superclass and therefore the class that doesn't inherited is subclass. Therefore, a subclass could be a specialized version of a superclass. It inherits all of the members defined by the superclass and adds its own, unique elements.

Example Of Inheritance:

               class Cow{
                          int a, b;
                          public void add(int x, int y)
                 {
                          a = x;
                          b = y;
                         System.out.println("addition of a + b is:" + (a + b));
                 }
                 }
                        class Calf extends Cow {
                        public void sum(int x, int y)
                {
                        add(x, y);
                 }
 
                       // Driver Code
                      public static void main(String[] args)
                {
                      Calf b1 = new Calf();
                      b1.sum(5, 6);
                }
}

Output:

                 addition of a+b is : 11

Here class Calf is the derived class which inherit the property (add method) of the base class Cow.


Polymorphism 

            Polymorphism (from Greek, meaning “many forms”) may be a feature that enables one interface to be used for a general class of action. Polymorphism in Java may be a concept by which we are able to perform one action in numerous ways.

it's applied to the functions or methods. Polymorphism allows the object to determine which sort of the function to implement at compile-time likewise as run-time.

Types of Polymorphism are:


1) Compile-time polymorphism (Method overloading)

This compile time polymorphism means binding is going on at compile time and this could be achieved by static binding. during this compile time polymorphism Inheritance isn't involved and method overloading is an example of compile time polymorphism.


2) Run-time polymorphism (Method Overriding)

Run time polymorphism where at run time we came to understand which method is going to invoke, it may be achieved through dynamic binding. during this run-time polymorphism Inheritance is involved and Method overriding is an example of runtime polymorphism.

Example Of  Polymorphism:

              class A {

                           int a, b, c;

                          public void add(int x, int y)

                    {

                          a = x;

                           b = y;

                           System.out.println("addition of a+b is:" + (a + b));

                     }

                           public void add(int x, int y, int z)

                    {

                            a = x;

                            b = y;

                            c = z;

                            System.out.println("addition of a+b+c is:" + (a + b + c));

                    }

                            public void print()

                    {

                            System.out.println("Class A's method is running");

                    }

                    };

                class B extends A {

                           public void print()

                   {

                           System.out.println("Class B's method is running");

                    }

                           // Driver Code

                           public static void main(String[] args)

                    {

                           A a1 = new A();


            // method overloading (Compile-time polymorphism)

                           a1.add(6, 5);

 

           // method overloading (Compile-time polymorphism)

                           a1.add(1, 2, 3);

                           B b1 = new B();


           // Method overriding (Run-time polymorphism)

                            b1.print();

                    }

                    }

Output:

                  addition of a+b is: 11

                  addition of a+b+c is:6

                Class B's method is running 

               


  • In Inheritance during which we create a new class that inherits the features from the already existing class whereas in polymorphism it are often defined in multiple forms.
  • Basically Inheritance is applied to classes and polymorphism is applied to functions or methods.
  • Single, multiple, multilevel, hierarchical, and hybrid testamentary are the various sorts of inheritance. Polymorphism, on the opposite extreme, is characterized as overloading and overriding.
  • In object oriented programming Inheritance supports the property of reusability and reduces the length of code whereas in polymorphism it permits to the thing to determine which type of the function to implement at compile time (overloading) also as run time (overriding).
  • In object oriented programming Inheritance is employed in pattern designing while polymorphism is additionally utilized in pattern designing.

        Both polymorphism and inheritance appear to be crucial elements in making any program a reality. They are, in fact, the foundations upon which object-oriented computing was created.

        One thing to remember when developing software is that inheritance must be used if you want to remodel data you've already created, essentially the specification of 1 category, and so do it again in your program to fulfil a similar or different function. you must use polymorphism if you would like to scale back general confusion in your code and use the identical context menu for related actions.


Blog By,

Neha Dhere 76,
Parth Ghube 81,
   Anurag Jadhav 86,
Niraj Kurane 94,
Om Mali 96,
 Rohan Pawar 101

SY Mechanical 
Division C
Vishwakarma Institute Of Technology, Pune.


References:

Comments

Post a Comment