Polymorphism, which means "many forms," is the ability to treat an object of any subclass of a base class as if it were an object of the base class Object variables are polymorphic. A variable of type Employee can refer to an object of type Employee or an object of any subclass of the Employee class (such as Manager, Executive, Secretary, and so on). A base class has, therefore, many forms: the base class itself, and any of its subclasses.
Subclasses of a class can define their own unique behaviors and yet share some of the same functionality of the parent class.
Liquid myFavoriteBeverage = new Liquid();
The myFavoriteBeverage variable holds a reference to a Liquid object. This is a sensible arrangement;
however, there is another possibility brought to you courtesy of polymorphism. Because of polymorphism, you can assign a reference to any object that is-a Liquid to a variable of type Liquid. So, assuming the inheritance hierarchy shown in Figure 8-1, either of the following assignments will also work:
Liquid myFavoriteBeverage = new Coffee();
// or... Liquid myFavoriteBeverage = new Milk();
Therefore, you can sprinkle some polymorphism in your Java program simply by using a variable with a base type to hold a reference to an object of a derived type.
To fully realize the wonders of polymorphism, you must send a message to an object without knowing the actual class of the object. To do this in Java, you just invoke a method defined in a base type on an object referenced by a variable of the base type. As you saw above, the object referred to by a base class reference might be of the base class or any of its subclasses. Therefore, when you write the code to invoke the method, you don't necessarily know the actual class of the object. Likewise, when you compile the code, the compiler doesn't necessarily know the actual class of the object. At run-time, the Java Virtual Machine determines the actual class of the object each time the method invocation is requested by your program. Based on this information, the Java Virtual Machine invokes the method implementation belonging to the object's actual class. Letting the Java Virtual Machine determine which method implementation to invoke, based on the actual class of the object, is how you realize the full power of polymorphism in your programs.
public class TestBikes {
public static void main(String[] args){
Bicycle bike01, bike02, bike03;
bike01 = new Bicycle(20, 10, 1); //Polymorphism too.
bike02 = new MountainBike(20, 10, 5, "Dual"); //Polymorphism
bike03 = new RoadBike(40, 20, 8, 23); bike01.printDescription(); //Polymorphism
bike02.printDescription(); //Dynamic Dispatch
bike03.printDescription(); //Dynamic Dispatch
}
bike02 = new MountainBike(20, 10, 5, "Dual"); //Polymorphism
bike03 = new RoadBike(40, 20, 8, 23); bike01.printDescription(); //Polymorphism
bike02.printDescription(); //Dynamic Dispatch
bike03.printDescription(); //Dynamic Dispatch
}
}
Confusion at polymorphism :
- There is no any run-time polymorphism because polymorphism is achieved at run-time automatically by the JVM, if not then it is not polymorphism.
- Method overriding is always connected to polymorphism but this is false (source: oracle.com). But is used to get full benefits of polymorphism and dynamic dispatch.
1 comments:
polymorphism in java
Thanks for this blog
Post a Comment