The benefits of polymorphism

When I started looking for the benefits of polymorphism, I found this question here. But here I could not find my answer. Let me tell you what I want to find. Here I have several 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 I have created two objects in the Demo class and am referencing Refrigerator . I fully understood that from the rf object I can call the trip() Refrigerator method, but this method will be hidden for the cm object. Now my question is why should I use polymorphism or why should I use

 CoolingMachines cm = new Refrigerator(); 

when i'm ok with

 Refrigerator rf = new Refrigerator(); 

Is the efficiency of polymorphic objects good or light in weight? What is the main purpose and difference between both of these objects? Is there a difference between cm.start(); and rf.start() ?

+68
java polymorphism java-ee oop
Jun 18 '12 at 12:14
source share
9 answers

This is useful when you are processing lists ... A brief example:

 List<CoolingMachines> coolingMachines = ... // a list of CoolingMachines for (CoolingMachine current : coolingMachines) { current.start(); } 

Or, if you want to allow the method to work with any subclass of CoolingMachines

+69
Jun 18 '12 at 12:19
source share

In cases where you really know a particular class well, there is no benefit. However, in many cases, you want to be able to write code that knows only about the base class or interface.

For example, look at Iterables in Guava - that there are many methods that (mostly) don't care which implementation of Iterable used. Do you really want all this code to be separate for each implementation?

Where you can code an abstract base class or interface, you allow yourself later to use other implementations that use the same open API, but can have different implementations. Even if you need only one production implementation, you may need an alternative implementation for testing. (The degree to which this applies is very dependent on the class in question.)

+55
Jun 18 '12 at 12:18
source share

Since later, if you want to use AirConditioner instead of Refrigerator for cooling, then only the code you need to change is CoolingMachines cm = new AirConditioner();

+28
Jun 18 '12 at 12:19
source share

The reason you want to use

 CoolingMachines cm = new Refrigerator(); 

is that you can easily use another CoolingMachines . You only need to change this line of code, and the rest of the code will work (because it will only use CoolingMachines methods, which are more general than a specific machine, for example, Refrigerator ).

So, for a specific instance of the Refrigerator calls cm.start(); and rf.start() work the same, but cm can also be another CoolingMachines object. And this object may have a different implementation of start() .

+16
Jun 18 '12 at 12:18
source share

First answer:

Use polymorphism to override a method and overload a method. Other class methods used in a different class, then two options: the first method is inherited, the second method is written above. Extend the interface here: use them or the implementation method: logically write them. The polymorphism used for the method is class inheritance.

Second answer:

Is there a difference between cm.start(); and rf.start(); ?

Yes, both are objects that are completely different in relation to each other. Do not create interface objects because Java does not support interface objects. The first object is created for the interface and the second for the class of refrigerators. The second object right now.

+7
Jun 18 '12 at 12:53 on
source share

The most general answer to the general part of your question (why should I use polymorphism?) Is that polymorphism implements several critical object-oriented design principles, for example:

  • code reuse: Having placed any code that is common to all of your β€œchilling machines” in the chilling machine, you only need to write this code once, and any changes to this code will instantly expire.

  • abstraction: The human brain can track only so many things, but they are good in categories and hierarchies. This helps to understand what is happening in a large program.

  • Encapsulation: each class hides the details of what it does, and is simply based on the interface of the base class.

  • separation of problems: a lot of object-oriented programming is associated with assignment of duties. Who will be responsible for this? Specialized problems can be performed in subclasses.

Thus, polymorphism is part of the larger picture of the oo, and the reasons for using it sometimes make sense only if you try to do "real" oo programming.

+5
Jun 18 '12 at 21:50
source share

A simple polymorphism option is that you can have a cooling array, where element 0 is the refrigerator, and element 1 is the AirConditioner, etc.

You do not need to perform any checks or make sure which object you are dealing with in order to trigger a trip or start, etc.

This can be a big advantage when receiving input from the user and the need to iterate over all objects and call similar functions

+3
Jun 18 '12 at 12:21
source share

Using your objects polymorphically also helps to create factories or families of related classes, which is an important part of how the Factory design pattern . Here is a very simple example of a polymorphic factory:

 public CoolingMachine CreateCoolingMachines(string machineType) { if(machineType == "ref") return new Refrigerator(); //other else ifs to return other types of CoolingMachine family } 

using call over code:

 CoolingMachine cm = CreateCoolingMachine("AC"); //the cm variable will have a reference to Airconditioner class which is returned by CreateCoolingMachines() method polymorphically 

Also imagine that you have a method as shown below that uses a specific parameter of the Refrigerator class:

 public void UseObject(Refrigerator refObject) { //Implementation to use Refrigerator object only } 

Now, if you change the implementation of the UseObject() method UseObject() to use the most general parameter of the base class, the calling code will have the advantage of passing any parameter, polymorphically, which can then be used inside the UseObject() method:

 public void UseObject(CoolingMachine coolingMachineObject) { //Implementation to use Generic object and all derived objects } 

The above code is now more extensible, since other subclasses can be added later to the CoolingMachines family and the objects of these new subclasses will also work with existing code.

+2
Jun 18 2018-12-12T00:
source share

I will give a simple example to understand. Let's say you have json

 {"a":[1,2],"sz":"text", "v":3, "f":1.2} 

Now we can say that programmatically you want to specify a name, type and value. Instead of having a switch () for each type (array for a, string for sz, etc.), you can simply have a base type and call a function that does its job. It is also more efficient with efficiency than with a dozen types of switches.

Then there are plugins, libs and external code with an interface.

+2
Jun 18 2018-12-18T00:
source share



All Articles