We learnt almost Encapsulation In previous post. Polymorphism Is to a greater extent than or less other OOP cardinal concept inwards coffee software development. Interviewer tin likewise enquire yous almost Polymorphism In coffee software evolution language then yous must locomote aware almost It.
What Is Polymorphism?
Polymorphism Is made from 2 Greek words -> "polys" in addition to "morphē". polys means "many, much" in addition to morphē means "form, shape". Polymorphism agency "many cast or many shapes". In normal words, Polymorphism Is the might past times which, We tin create reference variables or functions which behaves differently inwards dissimilar programmatic context.
Simple event of polymorphism Is cat makes audio "meow" in addition to domestic dog makes audio "woof". Thus, audio of makeSound() business office volition depends on the type of animal. There are 2 types of polymorphisms every bit bellow inwards coffee software evolution language.
1) Compile fourth dimension polymorphism
Compile fourth dimension polymorphism Is likewise known as static binding or method overloading. Static polymorphism Is achieved through method overloading. Method overloading agency at that spot are several methods amongst same advert In same cast but with different types/order/number of parameters. In this situation, Java knows compile fourth dimension which method needs to Invoke based on Its signature in addition to thus It Is known every bit compile fourth dimension polymorphism.
Let's empathize It amongst real basic event every bit bellow.
StaticPolymorph.java
package oopbasics; world cast StaticPolymorph { //Method 1 world int sum(int x, int y) { supply x + y; } //Method 2 world int sum(int x, int y, int z) { supply x + y + z; } //Method 3 world int sum(double x, int y) { supply (int) x + y; } //Method 4 world int sum(int x, double y) { supply x + (int) y; } }
Above cast has full iv methods overloaded amongst same advert sum. Now lets access all of them using dissimilar parameters in addition to types every bit bellow.
ExcStaticPolymorph.java
package oopbasics; world cast ExcStaticPolymorph { world static void main(String[] args) { StaticPolymorph poly = novel StaticPolymorph(); // Call Method 1 System.out.println(poly.sum(1, 7)); // Call Method 2 System.out.println(poly.sum(4, 2, 1)); // Call Method 3 System.out.println(poly.sum(2.5, 3)); // Call Method 4 System.out.println(poly.sum(4, 3.7)); } }
Output :
8 seven v 7
Above cast volition access total method from StaticPolymorph.java cast based on lay out of parameters in addition to Its type.
2) Run fourth dimension polymorphism
Run fourth dimension polymorphism Is likewise known as dynamic binding or method overriding inwards coffee software evolution language. You tin acquire method overriding when yous work Inheritance In your program. In this situation, Java knows run fourth dimension which method needs to Invoke. Let's empathize It using real unproblematic event of bring upwards cast Animal.java in addition to Its kid cast Dog.java every bit bellow.
Animal.java
package oopbasics; world cast Animal { world void makeNoise() { System.out.println("Some audio of animal."); } }
Dog.java
package oopbasics; cast Dog extends Animal { world void makeNoise() { System.out.println("Woof"); } }
Both inwards a higher house cast has method amongst same name makeNoise(). Animal.java Is bring upwards cast in addition to Dog.java Is Its kid class. Now lets access both methods every bit bellow.
ExcAnimalAndDog.java
package oopbasics; world cast ExcAnimalAndDog { world static void main(String[] args) { Animal a1 = novel Animal(); // Call makeNoise() from Animal class a1.makeNoise(); Animal a2 = novel Dog(); // Call makeNoise() from Dog class a2.makeNoise(); } }
Output :
Some audio of animal. Woof
This Is run fourth dimension polymorphism In coffee software evolution language. You tin work multiple children cast In inwards a higher house example. Next post service volition explicate yous abstract cast inwards java.