Polymorphism Concept
Polymorphism, a Greek word, where poly means a lot and metamorphosis means change, refers to the ability of an object to assume many forms.
The concept of polymorphism is often expressed as "an interface, several methods". When a general class can have the generic method and the derived classes (classes that extend the general class) they can add the class-specific implementation to that method. At the time of execution "An interface", that is, the general class will have "many forms", that is, references of the derived classes (see the example for clarity).
Note that the polymorphism is widely used with inheritance, where the methods of the main class are replaced by the daughter classes.
Polymorphism in Java
There are two types of polymorphism in java -
• Compilation time polymorphism (or static polymorphism)
• Run-time polymorphism (or dynamic polymorphism)
We can make polymorphism in java by
• Method overload (compile-time polymorphism) - In the method overload it is known at compile time itself what overloaded method will be called.
• Replace the method (runtime polymorphism) - In the substitution of the method, it is solved in the runtime, which will be called the substituted method. The decision is based on the reference of the object for which the reference variable is pointing.
We are going to see an example of runtime polymorphism in Java to understand the concept of polymorphism.
Here we have a parent Shape class with a method area (), the Shape class has two secondary classes: Square and Circle.
In Square class, area() method is overridden and it provides the implementation to calculate area of a square.
Same way area() method is overridden in Circle class to provide implementation to calculate area of a circle.
// Super Class
class Shape{
protected double length;
Shape(double length){
this.length = length;
}
void area(){
}
}
// Child class
class Square extends Shape{
//constructor to initialize length
Square(double side){
super(side); // calling the super class constructor
}
//Overriding the area() method
void area(){
System.out.println("In area method of square");
System.out.println("Area of square - " + length*length);
}
}
// Child class
class Circle extends Shape{
//constructor to initialize length
Circle(double radius){
super(radius); // calling the super class constructor
}
//Overriding the area() method
void area(){
System.out.println("In area method of circle");
System.out.println("Area of cirlce - " + 22/7*length*length);;
}
}
public class PolymorphicTest {
public static void main(String[] args){
Shape shape;
Square square = new Square(5.0);
Circle circle = new Circle(5.0);
// shape dynamically bound to the Square object referenced by square
shape = square;
// area method of the square called
shape.area();
// shape dynamically bound to the Circle object referenced by circle
shape = circle;
// area method of the circle called
shape.area();
}
}
Notice that in PolymorphicTest class' main method object shape of class Shape is declared and later that shape object takes the reference of square and then circle and calls the area method of the respective classes.
Output after running this program would be -
In area method of square
Area of square - 25.0
In area method of circle
Area of cirlce - 75.0
So it can be seen that the shape object holds the reference of the square object initially thus the area method of the Square class is called. Later the shape object holds the reference of the Circleobject thus the area method of the Circle class is called.
Points to be observed:
• Polymorphism, a Greek word, where poly means a lot and metamorphosis means change, therefore, refers to the ability of an object to assume many forms.
• Any number of classes can implement an interface and each class is free to provide its own implementation. Thus, using interfaces, Java fully uses the polymorphism aspect "an interface, several methods".
• There are two types of polymorphism in Java, polymorphism at compile time and polymorphism at runtime.
• Method overload is an example of compile-time polymorphism in Java.
• The overriding method is an example of runtime polymorphism in Java.
To getting expert-level training for Java Training in your location – java training in chennai | java training in bangalore | java training in pune | java training in chennai | java training in bangalore | java training in tambaram | java training in omr | java training in velachery | java training in annanagar | java training in chennai | java interview questions and answers | core java interview questions and answers | java training in marathahalli | java training in btm layout | java training in jayanagar | java training in chennai | java training in usa | For getting java online training | java online training
No comments:
Post a Comment