Answer the question
In order to leave comments, you need to log in
Why does polymorphism work like this?
Basically, I'm just trying to figure it out. As I understand it, there is, say, a superclass one and a class two struck from it. It turns out I want to write the following:
one x = new two();
I can only work with methods in two that do not affect the lines added in the affected class two. More precisely, they do not affect, but do not change, the lines in the child class, because I can specify in the object creation parameters what parameters I need to set and how I can work with them. Why and what is the meaning of polymorphism?
Answer the question
In order to leave comments, you need to log in
To explain the essence of polymorphism "on the fingers" is quite simple. Imagine a Phone class . It was designed by someone in the distant 80s, and defined in it the dialNumber() method . And then another programmer in the 90s inherited the MobilePhone class from him and overridden the dialNumber() method , because Roughly speaking, in the new device, dialing is no longer impulsive, but tonal. And then the third programmer inherited the Smartphone class from him . At the same time, he did not touch the dialNumber() method , but simply added methods for new functionality, such as determinePositionByGPS() , etc.
Now imagine a user. He comes from the 80's and has no idea about tone dialing and GPS... but if you put any of these devices in his hands, he can dial a number and make a call. Why? Because he knows how to use the dialNumber() method , and he doesn't need to know more.
Now imagine another user, our contemporary. He grew up in the era of smartphones... but if you give him an old telephone set, he will also be able to make a call, because, again, he knows the dialNumber() method .
In OOP terms, a user is a variable that contains a reference to an instance of a class. Its type (as it was declared) is a "collection of knowledge" about the capabilities of that instance. And because A smartphone is basically a phone, we can put it in the hands of a hypothetical user from the 80s:
as a result, he will be able to make a call:
But he will not be able to determine his location until he learns about the existence of the corresponding method:
This is called type casting. In this example, the class instance already had acc. method, but in order to use it, you must first explicitly indicate that we want to consider the device we have in our hands not as a "simple" Phone, but as a Smartphone.
PS By the way, in this example we touched not only on polymorphism, but also on inheritance and encapsulation (the "outside" user is not at all interested in how exactly the number is dialed - tone, impulse, or some other way) ... so to speak, everything three OOP kits in one bottle. And this is the only way it makes sense to consider these principles, because. they are essentially inseparable from each other, like the Father, Son and Holy Spirit in Christianity, or length, width and height in three-dimensional space :) If this is understood, OOP becomes a completely simple and natural programming paradigm.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question