Answer the question
In order to leave comments, you need to log in
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
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:
класс Автомобиль
свойство Высота
свойство Ширина
свойство ТипКузова
класс Автомойка
метод Помыть(Автомобиль автомобиль)
настроитьМойкуПоВысоте(автомобиль.Высота)
настроитьМойкуПоШирине(автомобиль.Ширина)
настроитьМойкуПоТипуКузова(автомобиль.ТипКузова)
начатьМойку()
Correct (in the sense of OOP). True, initially the interpretation was a little different: the ability to pass various data types to the function.
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");
}
}
polymorphism is stupid OVERDEFINITION of methods in classes
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question