L
L
link002014-12-13 23:32:57
PHP
link00, 2014-12-13 23:32:57

Am I understanding polymorphism correctly?

Read about polymorphism. Do I understand correctly that polymorphism is a stupid REDEFINITION of methods in classes? There is an abstract class. There are several, so to speak, "adjacent" classes that are inherited from it. By overriding the methods of "adjacent" classes, we achieve a "different implementation". (Well, of course, in such a way that the idea of ​​​​the interface is carried out. For example, there is an abstract class "transport". It has methods - forward, backward, left, right. We do not know what transport objects will be, say, in a 3D "box" of the world of GTA / etc. games.For cars, these movements will be implemented in one way, for a boat, an airplane in a different way.Another acceleration/modes (eg with/without nitrous oxide, shifting gears)

Answer the question

In order to leave comments, you need to log in

4 answer(s)
A
Alexander Bindyu, 2014-12-18
@link00

Reasonable goals when using polymorphism:
The bottom line is that, due to abstraction, you can pass different objects to the same methods, and it will not matter to you which particular object came into the method.
I'll give you an example. Imagine that you have a car wash - class Car Wash - that can only work for the yellow Lada Kalina. Other cars in it cannot be washed. The class has a method WashYellowLadaKalina (YellowLadaKalina car) . Those. there is a specific method that accepts a specific type of car.
At some point, you realize that black Volga could also be washed. You are building another input for the black Volga, i.e. you are creating another method in the class WashChernayaVolga(BlackVolga car) . Now your Car Wash class has expanded its capabilities.
You will be adding a new method every time there is a new type of car that you can wash. Most importantly, each of these methods will do almost the same thing. Here we have 2 problems:
What's the solution? It is necessary to highlight the characteristics of the car that are important for the Car Wash class and make one method Wash(Car car) . For example, this will be the height, width and body type of the car. This set of characteristics will be enough to wash any car.
We get:

класс Автомобиль
    свойство Высота
    свойство Ширина
    свойство ТипКузова

класс Автомойка
    метод Помыть(Автомобиль автомобиль)
        настроитьМойкуПоВысоте(автомобиль.Высота)
        настроитьМойкуПоШирине(автомобиль.Ширина)
        настроитьМойкуПоТипуКузова(автомобиль.ТипКузова)
        начатьМойку()

Now, no matter how many different cars are added to the system, our Car Wash class will not change. We removed duplication and made our class more resistant to changes in the external environment. The Car class can have a successor of ZheltayaLadaKalina and it will have properties/methods of the base class. Because Since the Wash method works with an abstraction, it will call polymorphic properties/methods without knowing anything about the specific implementation and inheritance hierarchy that lies under the Car class.
When designing classes, you need to pay attention to one more principle blog.byndyu.ru/2009/10/blog-post_29.html, then the design of your system will be even more flexible.

S
svd71, 2014-12-13
@svd71

Correct (in the sense of OOP). True, initially the interpretation was a little different: the ability to pass various data types to the function.

N
Nubzilo, 2014-12-14
@Nubzilo

Also a question on the topic, tk. self-taught and do not fully understand what polymorphism is and what is the point of using it? By polymorphism, I mean such code, but I don’t know if this is true ...
Code in C #

void Main()
{
  List<IDriving> transports = new List<IDriving>();
  transports.Add(new Car());
  transports.Add(new Boat());
  foreach (IDriving transport in transports)
  {
    transport.MoveForward();
    transport.MoveBack();
  }
}

// Define other methods and classes here
public interface IDriving 
{
  void MoveForward();
  void MoveBack();
}

public class Car : IDriving
{
  public void MoveForward()
  {
    Console.WriteLine ("Car move forward");
  }
  public void MoveBack()
  {
    Console.WriteLine ("Car move back");
  }
}

public class Boat : IDriving
{
  public void MoveForward()
  {
    Console.WriteLine ("Boat move forward");
  }
  public void MoveBack()
  {
    Console.WriteLine ("Boat move back");
  }
}

Output:
Car move forward
Car move back
Boat move forward
Boat move back

H
haiku, 2014-12-24
@haiku

polymorphism is stupid OVERDEFINITION of methods in classes

No wrong. Polymorphism is primarily an "idea". There are different types of polymorphism. On Wikipedia, everything seems to be quite elementary described.
en.wikipedia.org/wiki/%D0%9F%D0%BE%D0%BB%D0%B8%D0%...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question