Sunday, November 24, 2019

What Is Polymorphism Inwards Java? Overriding Or Overloading?

Polymorphism vs Overloading vs Overriding
Someone asked me What are the departure betwixt Polymorphism in addition to Overriding inwards Java in addition to the similar difference betwixt Polymorphism in addition to Overloading. Well, they are non 2 dissimilar things, Polymorphism is an object oriented or OOPS concept similar Abstraction, Encapsulation or Inheritance which facilitate the purpose of the interface in addition to allows Java programme to accept payoff of dynamic binding inwards Java. Polymorphism is every bit good a way through which a Type tin deport differently than expected based upon which variety of Object it is pointing. Overloading in addition to overriding are 2 forms of Polymorphism available inwards Java.


Both overloading in addition to the overriding concept are applied on methods inwards Java. Since polymorphism literally agency taking multiple forms, So fifty-fifty though you lot direct maintain the advert of the method same inwards the instance of overloading in addition to overriding, an actual method called tin endure whatever of those multiple methods amongst the same name. Let's run across but about to a greater extent than details on method overloading in addition to overriding to sympathise how polymorphism relates to overloading in addition to overriding in addition to How they are different.


Polymorphism vs Overriding

Overriding is a cast of polymorphism which is used inwards Java to dynamically bind method from the subclass inwards reply to a method telephone phone from sub cast object referenced past times superclass type. Method overriding is bonded using dynamic binding inwards Java.  


Suppose you lot direct maintain 2 methods size() inwards both base of operations cast in addition to derived cast in addition to Base cast variable is pointing to an object which happens to endure subclass object at runtime thus method from subclass volition endure called, i.e. overridden method volition endure called.

This allows to program for interface than implementation, a pop OOPS pattern principle because Polymorphism guarantees to invoke right method based upon the object. Method overriding is cardinal for much flexible pattern pattern inwards Java.

See What is method overriding inwards Java  and Rules of method Overriding for examples in addition to to a greater extent than details.



Polymorphism vs Overloading

Method overloading is but about other cast of Polymorphism though but about people debate against that. In the instance of overloading, you lot every bit good got multiple methods amongst the same advert but dissimilar method signature but a telephone phone to right method is resolved at compile fourth dimension using static binding inwards Java. Overloading is a compile fourth dimension activity oppose to Overriding which is runtime activity. Because of this argue overloading is faster than method overriding inwards Java. Though beware amongst an overloaded method which creates conflict e.g. methods amongst solely i parameter e.g. int in addition to long etc. See What are method overloading inwards Java for representative in addition to consummate details.

Polymorphism vs Overloading vs Overriding What is  Polymorphism inwards Java? Overriding or Overloading?
An Example of Polymorphism inwards Java


An representative of Polymorphism inwards Java

subclass Dog in addition to Cat.


import java.util.ArrayList;
import java.util.List;

abstract class Pet{
    public abstract void makeSound();
}

class Cat extends Pet{

    @Override
    public void makeSound() {
        System.out.println("Meow");
    }  
}

class Dog extends Pet{

    @Override
    public void makeSound() {
        System.out.println("Woof");
    }
 
}

Let's show How Polymorphism concept function inwards Java:

/**
 *
 * Java programme to demonstrate What is Polymorphism
 * @author Javin Paul
 */

public class PolymorphismDemo{

    public static void main(String args[]) {
        //Now Pet volition present How Polymorphism function inwards Java
        List<Pet> pets = new ArrayList<Pet>();
        pets.add(new Cat());
        pets.add(new Dog());
     
        //pet variable which is type of Pet deport dissimilar based
        //upon whether pet is Cat or Dog
        for(Pet pet : pets){
            pet.makeSound();
        }
   
    }
}

Output:
Meow
Woof

In Summary, you tin non compare Polymorphism amongst method overloading or override. Polymorphism is the mightiness of a variable to deport differently based upon which variety of Object it is referring. They are Java programming language's way to implement polymorphism inwards language.


Other Java in addition to OOPS tutorial for programmers

No comments:

Post a Comment