Polymorphism means one name, many forms. There are 3 distinct forms of Java Polymorphism;
Method overloading (Compile time polymorphism)
Method overriding through inheritance (Run time polymorphism)
Method overriding through the Java interface (Run time polymorphism)
Polymorphism allows a reference to denote objects of different types at different times during execution. A super type reference exhibits polymorphic behavior, since it can denote objects of its subtypes.
interface Shape {
public double area();
public double volume();
}
class Cube implements Shape {
int x = 10;
public double area() {
return (6 * x * x);
}
public double volume() {
return (x * x * x);
}
}
class Circle implements Shape {
int radius = 10;
public double area() {
return (Math.PI * radius * radius);
}
public double volume() {
return 0;
}
}
public class PolymorphismTest {
public static void main(String args[]) {
Shape[] s = { new Cube(), new Circle() };
for (int i = 0; i < s.length; i++) {
System.out.println("The area and volume of " + s[i].getClass()
+ " is " + s[i].area() + " , " + s[i].volume());
}
}
}
Output:
The area and volume of class Cube is 600.0 , 1000.0
The area and volume of class Circle is 314.1592653589793 , 0.0
The methods area() and volume() are overridden in the implementing classes. The invocation of the both methods area and volume is determined based on run time polymorphism of the current object as shown in the output.
1 comments:
And indeed, I’m just always astounded concerning the remarkable things served by you. Some four facts on this page are undeniably the most effective I’ve had.
Jmeter training in chennai | best jmeter training in chennai | jmeter training institute in chennai | jmeter training institute in chennai KK Nagar
php training institute in chennai | php training in chennai |best php training institute in chennai |php course in chennai |best php training in chennai | best php training in chennai KK Nagar
angularjs training in chennai | angularjs course in chennai |angularjs training institute in chennai |angularjs training institutes in chennai | angularjs training institutes in chennai KK Nagar
Best selenium training institute in chennai | selenium course in chennai | selenium training institute in chennai | selenium classes in chennai | selenium testing training in chennai |selenium certification in chennai | selenium certification in chennai KK Nagar
Post a Comment