Leo
Leo

Reputation: 5235

Is it really polymorphism?

Consider an interface called Shape which has a draw() method. Two classes Triangle and Circle implements the Shape interface and override the draw() method. Now in the main I have the following piece of code:

public static void main(String[] args){

    Shape shape = new Triangle();
    shape.draw();

    //Shape shape = new Circle();
    //shape.draw();
}

I find this to be an example of polymorphism as we don't know which draw method will be invoked at the runtime. The explanation says While invoking shape.draw() the compiler sees draw() in the Shape interface at compile time, and the JVM invokes draw() in the Triangle class at run time. Same happens for the draw() in the Circle class.

My doubt is, can this be actually called polymorphism? As the new Triangle() or new Circle() hard coded, won't the compiler always know that it is pointing to the child classes' draw() method?

Upvotes: 4

Views: 147

Answers (5)

Vitaly
Vitaly

Reputation: 2662

Let's write the same example but with array:

public static void main(String[] args){
    Shape[] shapes = new Shape[2];
    shapes[0] = new Triangle();    
    shapes[1] = new Circle();
    for(int i=0;i<shapes.length;++i){
        shape.draw();
    }
}

From my point of view, it is better illustration of polymorphism. Hope, it will help you.

Upvotes: 0

Rajasekhar
Rajasekhar

Reputation: 807

For achieving polymorphism you can try this

    public static void main(String[] args){

        SampleClass sample=new SampleClass(new Triangle() or new Circle())
                sample.draw();
       }

    Clas SampleClass
    {
    Shape shape;
    //constructor
     public SampleClass(Shape shape)
    {
         this.shape=shape;

     }
       public void draw()
     {
            shape.draw();
      }

}

Upvotes: 0

BatScream
BatScream

Reputation: 19700

The concept of Runtime polymorphism is best explained with a Factory method, that returns a Shape object based on an input.

Factory method:

public static Shape getShape(int i){ 
              if(i == 1) {return new Triangle();}
              else{return new Circle();}  
}

Property file:

num:1

Based on the value in the property file, a Shape object is obtained.

public static void main(String[] args){

   int x = getFromPropertyFile();
   Shape shape = getShape(x); // Shape obtained from a factory method
   shape.draw(); //Runtime polymorphism
}

The compiler has no idea which object would be returned. It is determined at Run time by the value provided as input. You can see this kind of implementation in JDBC driver implementations, where the implementation class is determined at run time.

In Your example:

Shape shape = new Triangle();
shape.draw();

Shape shape = new Circle();
shape.draw();

The method binding happens at the compile time i.e which methods can be invoked on a given reference type is decided at the compile time.

The selection of the method’s implementation to execute happens at the run time i.e which implementation of the method to be executed i.e the super class version or one of the subclass’s version is decided at the run time.

Upvotes: 4

Techfist
Techfist

Reputation: 4344

A, you are referring child objects with parent reference, so there is know way to know which draw() to called in compile time.

Moreover, your compiler didn't threw any error and let your code get compiled, cause it knows shape has an method name as draw and not your Triangle or Circle has that or not, as for compiler only thing which mattered was accessibility of method draw when you do shape.draw(). You can try putting one method only in Triangle and try calling that up using shape.xxx compiler wont allow the same.

Now, having said that above two things can be deduced, A. we saw polymorphism which can be resolved in compile time we call that as static polymorphism(e.g your compiler allowed you to access child reference via parent) B. in runtime although you said shape.draw, then also instead of calling parent draw it called child draw, this can be resolved only in runtime, so naming it as runtime polymorphism.

I hope I cleared you doubt.

Upvotes: 0

null
null

Reputation: 161

I think the more proper example is

List<Shape> lstShape = new ArrayList<Shape>();
lstShape.add(new Circle());
lstShape.add(new Triangle());

...

for (Shape s: lstShape) {
    s.draw();
}

Also see: http://en.wikipedia.org/wiki/Polymorphism_%28computer_science%29

Upvotes: 1

Related Questions