-5

Possible Duplicate:
What is Polymorphism?

I am currently learning about polymorphism in Java and I can't comprehend how it would be useful. I keep seeing people say if you wanted to make a circle or square then you would make a superclass called "shape" so then you can call the separate circle and square class's but why not just create separate objects?

Community
  • 1
  • 1
nPwn
  • 13
  • 5
  • 2
    This has been asked numerous time, please consider using the search. – KyelJmD Dec 31 '12 at 03:31
  • Like I said, I don't understand the other answers. – nPwn Dec 31 '12 at 03:33
  • What about the other answers confuse you? How will we know what to say if you won't tell us this? What's to prevent a reiteration of that which has been well stated before if you refuse to clarify your problem? – Hovercraft Full Of Eels Dec 31 '12 at 03:34
  • I know lua but as I'm getting in to this, I don't understand because there are other ways to do the exact same thing yet people say that this is so important. – nPwn Dec 31 '12 at 03:37
  • Well in biology Polymorphism is the ability of an organism or specie can have many different forms or stages. in object oriented programming. same concept goes with programming, an Objecting having many forms. – KyelJmD Dec 31 '12 at 03:39
  • Geez, just look for examples in the JDK itself. – duffymo Dec 31 '12 at 03:41
  • He would fully understand if he tries it himself. – KyelJmD Dec 31 '12 at 04:01

2 Answers2

1

If your circle and square have a method render(), then somebody can implement an object renderer like this:

class Renderer {
    Canvas c;

    public Renderer(Canvas c) {
        this.c = c;
    }

    public void draw(Shape shape) {
        shape.render(c);
    }
}

so the Renderer can draw on any Canvas (or subclasses of Canvas), any Shape that you can provide.

Differently - without polymorphism - you must have written something like:

class Renderer {
    Canvas c;

    public Renderer(Canvas c) {
        this.c = c;
    }

    public void draw(Circle circle) {
        circle.render(c);
    }

    public void draw(Square square) {
        square.render(c);
    }

    public void draw(Triangle square) {
        triangle.render(c);
    }
    // and so on...
}

Then you may think, what if I have different type of Canvases? PaperCanvas, WoodCanvas, ClothCanvas and so on... The renderer and the Shapes can still use the abstraction Canvas to interact with that object, without knowing the implementation details and all the possible types of Canvas they may be drawn to.

Luigi R. Viggiano
  • 8,659
  • 7
  • 53
  • 66
  • Okay, that makes sense. But those separate methods for each shape would have to be in separate classes. So why not just make one class that has all the methods in it and just call each method in that class? – nPwn Dec 31 '12 at 03:48
  • @nPwn - Try it yourself ... and see what happens. Can you maintain encapsulation? How many classes needed to be changed when you decide to implement (say) a `Pentagon` class? – Stephen C Dec 31 '12 at 04:52
1

Well in biology Polymorphism is the ability of an organism or specie can have many different forms or stages. in object oriented programming. same concept goes with programming, An object can exist in many forms.

Let's say you have this Employee class.

public class Employee{
   public int computeSalary(){
       //How would you compute the salary of an average employee
    }
}

Next is you have a subclass called BasePlusComission Employee

 public class BasePlusCommissionEmployee extends Employee{
          public int computeSalary(){
               //Commision employee salary computation + Base Employee salary computation
            }
}

And you have this CommissionEmployee

 public class CommissionEmployee extends Employee{
              public int computeSalary(){
                   //Commision employee salary computation
                }
    }

Now, we want to handle the computation of this Employees polymorphically. we can do

public void computeEmployeeSalary(Employee emp){
     emp.computeSalary();
}

This method will receive any kind of Employee object(which means that we can pass a subclass of the Employee object). Once you have passed it, it will compute the passed employee (the argument) and it's computeSalary() will be called,

Even though you pass a BasePlusCommission Employee, or CommissionEmployee, it will compute its salary depending on its implementation thanks to Dynamic method look up.

Another clearer explanation. you can create a set or an array of employees.

Employee[] employees = { new BasePlusComissionEmployee(), new CommissionEmployee,new Employee()};

and you want these objects to compute their salary. you can handle their computation polymorphically like this

for(int i = 0; i < employees.length;i++){
    employees[i].computeSalary();
}

Take note that Even though the arrays declaration is Employee their computeSalary() implementation will vary depending on each class implementation of computeSalary(); of each objects of the employee array

KyelJmD
  • 4,682
  • 9
  • 54
  • 77