Benefit of Polymorphism
Solution 1
It is useful when you handle lists... A short example:
List<CoolingMachines> coolingMachines = ... // a list of CoolingMachines
for (CoolingMachine current : coolingMachines) {
current.start();
}
Or when you want to allow a method to work with any subclass of CoolingMachines
Solution 2
In cases where you're really okay with knowing the concrete class, there's no benefit. However, in many cases you want to be able to write code which only knows about the base class or interface.
For example, look at Iterables
in Guava - that's a lot of methods which (mostly) don't care what implementation of Iterable
is being used. Would you really want all that code separately for every implementation?
Where you can code to an abstract base class or an interface, you allow yourself to later use other implementations which share the same public API, but may have different implementations. Even if you only want a single production implementation, you may well want alternative implementations for testing. (The extent to which this applies very much depends on the class in question.)
Solution 3
Because later if you want to use AirConditioner
instead of Refrigerator
for cooling, then only code you need to change is CoolingMachines cm = new AirConditioner();
Solution 4
The reason that you want to use
CoolingMachines cm = new Refrigerator();
is that you can later easily use a different CoolingMachines
. You only need to change that one line of code and the rest of the code will still work (as it will only use methods of CoolingMachines
, which is more general than a specific machine, such as a Refrigerator
).
So for a particular instance of Refrigerator
, the calls cm.start();
and rf.start()
work the same way but cm
might also be a different CoolingMachines
object. And that object could have a different implementation of start()
.
Solution 5
First answer:
Use polymorphism for method overridding and method overloading. Other class methods used in different class then two options: first method inherited, second method over written. Here extend interface: use them, or implemention method: logic write them. Polymorphism used for method, class inheritance.
Second answer:
Is there any difference between cm.start();
and rf.start();
?
Yes, both are objects that are completely different with respect to each other. Do not create interface objects because Java doesn`t support interface objects. First object created for interface and second for Refrigerator class. Second object right now.
khan
I am a learner Who wants to learn All programming as well as Human Languages ....
Updated on June 19, 2020Comments
-
khan almost 4 years
When I started to look for the benefits of polymorphism, I found with this question here. But here I was unable to find my answer. Let me tell what I want to find. Here I have some classes:
class CoolingMachines{ public void startMachine(){ //No implementationion } public void stopMachine(){ //No implementationion } } class Refrigerator extends CoolingMachines{ public void startMachine(){ System.out.println("Refrigerator Starts"); } public void stopMachine(){ System.out.println("Refrigerator Stop"); } public void trip(){ System.out.println("Refrigerator Trip"); } } class AirConditioner extends CoolingMachines{ public void startMachine(){ System.out.println("AC Starts"); } public void stopMachine(){ System.out.println("AC Stop"); } } public class PolymorphismDemo { CoolingMachines cm = new Refrigerator(); Refrigerator rf = new Refrigerator(); }
Now here I created two objects in the Demo class and are references of
Refrigerator
. I have completely understood that from therf
object I am able to call thetrip()
method ofRefrigerator
, but that method will be hidden for thecm
object. Now my question is why should I use polymorphism or why should I useCoolingMachines cm = new Refrigerator();
when I am OK with
Refrigerator rf = new Refrigerator();
Is polymorphic object's efficiency is good or light in weight? What is the basic purpose and difference between both of these objects? Is there any difference between
cm.start();
andrf.start()
? -
user606723 almost 12 yearsThis is obviously just one example of how it can be useful. Basically, you can have common API.
-
Rohan Desai almost 12 yearsI think you got part of your explanation backwards. One uses factories as a means to allow for polymorphic code, not as an end for polymorphic code.
-
jcolebrand almost 12 yearsThis then let's us start using things like Dependency Injection, for example. Interface based programming has a huge number of advantages.
-
jcolebrand almost 12 yearsLet me introduce you to two of my closest friends,
Shift
andEnter
. Welcome to our community, but here we like to format things, rather than just keep typing blindly. Also, I need you to review the changes I made and ensure that they kept the intent of your post. Feel free to edit. I think you made some errors in your statements, but I'm not entirely sure. -
Wayne Werner almost 12 years@jcolebrand, like testing against interfaces, instead of implementation. Then your unit tests don't care whether the data is being pulled from a database, flat file, or thin air. Integration tests should, but that's a different story ;)
-
Donnie almost 12 yearsYeah, this is definitely only one example. For a more conceptual understanding it's better when designing an application that is scalable, readable, and may follow design patters (useful when designing class diagrams).
-
Timothy Baldridge almost 12 yearsI agree with missingo, Factories are simply a hack for dealing with the restrictions put into place by modern OOP languages like Java and C#.
-
VSS almost 12 years@missingno that is what also I am trying to say, Factories make use of Polymorphic code but there are other requirements of polymorphic code also..
-
Shehaaz about 11 yearsIt is also designed to obey the Liskov Substitution Principle: The principle states that if
S
is a subtype ofT
, THENT
could be substituted for every occurance ofS