Note that there are some explanatory texts on larger screens.

plurals
  1. POBenefit of Polymorphism
    primarykey
    data
    text
    <p>When I started to look for the benefits of polymorphism, I found with <a href="https://stackoverflow.com/questions/11064409/why-to-use-polymorphism">this</a> question here. But here I was unable to find my answer. Let me tell what I want to find. Here I have some classes:</p> <pre><code>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(); } </code></pre> <p>Now here I created two objects in the Demo class and are references of <code>Refrigerator</code>. I have completely understood that from the <code>rf</code> object I am able to call the <code>trip()</code> method of <code>Refrigerator</code>, but that method will be hidden for the <code>cm</code> object. Now my question is why should I use polymorphism or why should I use</p> <pre><code>CoolingMachines cm = new Refrigerator(); </code></pre> <p>when I am OK with</p> <pre><code>Refrigerator rf = new Refrigerator(); </code></pre> <p>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 <code>cm.start();</code> and <code>rf.start()</code>?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload