Why is this called polymorphism?

I watch this video , and 2.52 times it gives an example. He says that Shape can be an Interface or an abstract class, and there are other 2 classes, namely Triangle and Circle . Shape has a draw method.

Now he 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 performed either in the method overload or in the override method, as indicated in this article .

Can anyone tell if the example shown in the video is really polymorphism? Thank you in advance for any help.

+5
source share
5 answers

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

This is polymorphism, because the draw () method, apparently in the parent Shape class, is also overridden by several child classes: Triangle and Circle. Therefore, draw () has more than one shape: Circle and Triangle shapes.

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

0
source

Polymorphism means a condition that has many forms or the ability to take different forms. With this in mind, a form can take various forms, such as a triangle or a circle. These forms would have common properties and be encoded and used as such. As another example, to better understand, you can have a man, and a man can have several forms: a man and a woman.

 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 } 

Then you can create the floor you want using dynamic binding:

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

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

0
source

A triangle and a circle inherit from the Shape class. The Shape class may contain subclass objects. therefore there is polymorphism. you can say Triangle t = new Triangle (); and Circle c = new Circle ();

But here you have a superclass containing a subclass of Shape s = new Circle (); polymorphism - having many forms,

0
source

Polymorphism means that one object can take several forms. You can say Shape a = new triangle (), because the triangular shape is IS-A (the triangle extends the shape). You can also say Shape a = new Circle (), because Circle IS-A Shape (Circle extends Shape). On the first line of your example, the JVM will call the draw () method from the Triangle class. On the second line, he will call the draw () method defined in the Circle class, all the time using the Shape link!

 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"); } } 
0
source

Do not start too technical first, try to make sense. The meaning of polymorphism in OOP is the ability of an object to behave differently. now let's think about how method overloading is polymorphism

Method overloading is polymorphism

The OOP method shows the behavior using the method overload method in java, you can create a method with the same name but with a different list of parameters, now you can start thinking .. the same name means β†’ the same behavior, but we know, although the name is similar the behavior is not quite the same .. in simple polymorphic words

Example

: you have an β€œeat” method in the human class, now you are creating another reception method with a different list of parameters, according to which method you call expected behavior changes.

Method overriding is polymorphism

then how is method overriding a polymorphism? try to understand this. in the override method, you override the method that is defined in the superclass.

ex: the person has a food method, and now you create the SuperHuman class, which is a subclass of the person and then overrides the nutrition method

therefore, we know that SuperHuman also has the ability to eat, but in a different way, but not like overloading here now, we have the problem of demonstrating polymorphism. Why, because if you create an instance of a person, then there is one method, so polymorphic behavior does not exist. just as if you created an instance of SuperHuman, then there is one method and one expected behavior so that there is no polymorphism. therefore we demonstrate

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

we can’t just say what method does the output, looking only at the left side, because both β€œa” and β€œb” are a type of person, so the compiler cannot be sure what the output of a.eat () will be and b.eat () until the code runs, so that it is polymorphic.

0
source

Source: https://habr.com/ru/post/1211332/


All Articles