5

I am watching this video, and at 2.52 time it gives an example. It says Shape can be an Interface or an Abstract class and there are other 2 classes namely Triangle and Circle. Shape is having a method draw.

Now it says:

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

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

and claims that it is polymorphism.

But as far as I know polymorphism can be done either in method overloading or method overriding as given in this article.

Can somebody please tell is the example given in the video is really polymorphism? Thanks in advance in any kind of help.

halfer
  • 19,824
  • 17
  • 99
  • 186
  • 2
    Polymorphism is where a parent class can be a blueprint to many children objects. A triangle and circle both are a shape, and share common features, such as surface area. A person being a parent class, and male or female being the children objects is another example of polymorphism. – Drew Kennedy Jan 17 '15 at 17:58
  • 2
    It's polymorphism because `Shape` is the super class. – Shahar Jan 17 '15 at 17:59
  • Don't worry about it. I gave you an upvote because I actually thought it was a good question. Welcome to Stack Overflow. Learning our rules is a process and slipping up is human, especially if you're new. – La-comadreja Jan 17 '15 at 18:00
  • Wikipedia, third bullet: Subtyping (or inclusion polymorphism) is a concept wherein a name may denote instances of many different classes as long as they are related by some common superclass. In object-oriented programming, this is often referred to simply as polymorphism. - Searching the web may be preferable to an SO Q. – laune Jan 17 '15 at 18:00
  • possible duplicate of [Polymorphism vs Overriding vs Overloading](http://stackoverflow.com/questions/154577/polymorphism-vs-overriding-vs-overloading) – Julien Jan 17 '15 at 18:11

5 Answers5

0

There are several ways to write polymorphic structures in Java. Polymorphism is simply "the ability to create a variable, a function, or an object that has more than one form."

It is polymorphism because method draw(), apparently in parent class Shape, is also overridden by multiple child classes: Triangle and Circle. So draw() has more than one form: the Circle and Triangle forms.

See http://howtodoinjava.com/2013/07/15/what-is-polymorphism-in-java/

La-comadreja
  • 5,627
  • 11
  • 36
  • 64
0

Polymorphism means a state of having many shapes or the capacity to take on different forms. With this in mind, a shape can take many forms, such as a triangle or a circle. These forms would have shared properties, and are coded and used as such. As another example to maybe better help understand, you can have a person, and a person can have multiple forms: male and female.

public abstract class Person {
   private String name;
   private int age;
   //additional code - functionalities that children classes share
}

public class Male extends Person {
   //fields and functionalities that males only have
}

public class Female extends Person {
   //fields and functionalities that females only have
}

You can then create the gender you want through dynamic binding:

Person male = new Male();
Person female = new Female();

To answer your question, yes, the example provided to you is indeed Polymorphism.

Drew Kennedy
  • 4,118
  • 4
  • 24
  • 34
0

Triangle and circle both are inherited from Shape class. Shape class can hold objects of sub classes. so polymorphism is there. you can say Triangle t = new Triangle(); and Circle c = new Circle();

But here you have super class holding the sub class object Shape s= new Circle(); polymorphism- having many forms,

SMK
  • 324
  • 3
  • 14
0

Polymorphism means that one object can take on several forms. You can say Shape a = new Triangle() because a Triangle IS-A shape (Triangle extends Shape). You can also say Shape a = new Circle() because a Circle IS-A Shape (Circle extends Shape). In the first line of your example, the JVM will call the draw() method from the Triangle class. In the second line, it will call the draw() method defined in the Circle class, all the while using a Shape reference!

abstract class Shape {
    public abstract void draw();
}

class Triangle extends Shape {

    @Override
    public void draw() {
        System.out.println("Triangle");
    }
}

class Circle extends Shape {

    @Override
    public void draw() {
        System.out.println("Circle draw");
    }
}
Helenesh
  • 3,999
  • 2
  • 21
  • 36
0

Don't go too technical at first, try to understand from the meaning. Meaning of polymorphism in OOP is ability of object to behave differently. now lets think how method overloading represent polymorphism

Method Overloading is Polymorphism

In OOP method represents a behavior, using method overloading technique in java you can create a method which has the same name but different parameter list, now lets start thinking.. same name means --> same behavior but we know even though the name is similar behavior is not exactly the same.. in simple words polymorphic

example : you have eat method in human class now you create another eat method with different parameter list, according to which method you call expected behavior changes.

Method Overriding is Polymorphism

then how method overriding is polymorphism? lets try to figure this out. in method override you override a method which is defined in the super class.

ex : human has a eat method and now you create a SuperHuman class which is a subclass of the human and then override the eat method

so we know that SuperHuman is also having ability to eat but differently, but not like overloading now here we have a problem of demonstrating the polymorphism. Why because if you create a instance of human then there is one method so there is no polymorphic behavior. same as if you create a instance of SuperHuman then there is one method and one expected behavior so there is no polymorphism. so this is how we demonstrate

Human a = new Human();
a.eat();
Human b = new SuperHuman();
b.eat();

we can't simply say which method does which output by looking at only the left side because both "a" and "b" are Human type so compiler can't be certain what will be the output of a.eat() and b.eat() until code actually runs so it is polymorphic.

Imal Hasaranga Perera
  • 9,683
  • 3
  • 51
  • 41