We learnt about Encapsulation In previous post. Polymorphism Is another OOP fundamental concept in java software development. Interviewer can also ask you about Polymorphism In java software development language then you must be aware about It.
What Is Polymorphism?
Polymorphism Is made from two Greek words -> "polys" and "morphē". polys means "many, much" and morphē means "form, shape". Polymorphism means "many form or many shapes". In normal words, Polymorphism Is the ability by which, We can create reference variables or functions which behaves differently in different programmatic context.
Simple example of polymorphism Is cat makes sound "meow" and dog makes sound "woof". Thus, sound of makeSound() function will depends on the type of animal.
There are two types of polymorphisms as bellow in java software development language.
1) Compile time polymorphism
Compile time polymorphism Is also known as static binding or method overloading. Static polymorphism Is achieved through method overloading. Method overloading means there are several methods with same name In same class but with different types/order/number of parameters. In this situation, Java knows compile time which method needs to Invoke based on Its signature and so It Is known as compile time polymorphism.
Let's understand It with very basic example as bellow.
StaticPolymorph.java
package oopbasics;
public class StaticPolymorph {
//Method 1
public int sum(int x, int y) {
return x + y;
}
//Method 2
public int sum(int x, int y, int z) {
return x + y + z;
}
//Method 3
public int sum(double x, int y) {
return (int) x + y;
}
//Method 4
public int sum(int x, double y) {
return x + (int) y;
}
}
Above class has total four methods overloaded with same name sum. Now lets access all of them using different parameters and types as bellow.
ExcStaticPolymorph.java
package oopbasics;
public class ExcStaticPolymorph {
public static void main(String[] args) {
StaticPolymorph poly = new 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
7
5
7
Above class will access sum method from StaticPolymorph.java class based on number of parameters and Its type.
2) Run time polymorphism
Run time polymorphism Is also known as dynamic binding or method overriding in java software development language. You can get method overriding when you use Inheritance In your program. In this situation, Java knows run time which method needs to Invoke. Let's understand It using very simple example of parent class Animal.java and Its child class Dog.java as bellow.
Animal.java
package oopbasics;
public class Animal {
public void makeNoise() {
System.out.println("Some sound of animal.");
}
}
Dog.java
package oopbasics;
class Dog extends Animal {
public void makeNoise() {
System.out.println("Woof");
}
}
Both above class has method with same name makeNoise(). Animal.java Is parent class and Dog.java Is Its child class. Now lets access both methods as bellow.
ExcAnimalAndDog.java
package oopbasics;
public class ExcAnimalAndDog {
public static void main(String[] args) {
Animal a1 = new Animal();
// Call makeNoise() from Animal class
a1.makeNoise();
Animal a2 = new Dog();
// Call makeNoise() from Dog class
a2.makeNoise();
}
}
Output :
Some sound of animal.
Woof
This Is run time polymorphism In java software development language. You can use multiple children class In above example. Next post will explain you abstract class in java.
great...no words to express my gratitude..keep it up sir.
ReplyDeleteNice Tutorial.Easy to Understand, Waiting more for tutorial for advance Java.
ReplyDeleteWoof!Woof!
ReplyDeletethe guide is like the light leading me out of dark.
ReplyDelete