Greetings, friends. Welcome to ExtremeCode – extreme programming channel. khm, sorry (russian cough) Yeah, this is much better (speaking bout sound)
In this video we’ll discuss Polymorphism. It is actually quite difficult to give a definition for what polymorphism in programming is. But let’s say at this stage that polymorphism is an ability of something to change itself in some way. There are a lot of different kinds of polymorphism. But first of all let’s take a look at this simple scheme (ad hoc – parametric – subtype polymorphism)
Well, we don’t need the first two. At all. But SUBTYPE POLYMORPHISM is usually referred to as just ‘polymorphism’ in terms of OOP. And it is what we’re gonna speak about in this video. Authors of many books, articles and other ‘information sources’ (meh) call subtype polymorphism just ‘polymorphism’ call subtype polymorphism just ‘polymorphism’ It is correct in the context of OOP But keep in mind, that polymorphism in programming is not restricted to only object-oriented approach. Especially considering that C# also has two other polymorphism manifestations.
Spoiler : (operator overloading)
(Some Game Of Thrones spoilers) But we’ll speak of them later But let’s finally find out what does subtype polymorphism mean. It’s the ability of objects of the same specification to have different implementations. Very simple, isn’t it?
He-he. Well, everything’s actually not that difficult. Polymorphism principles are easy to understand after seeing a live example. Let’s set ourselves a task – to create a virtual zoo and force every animal to make a sound – to make sure it’s alive. Subtype polymorphism in OOP is closely connected to inheritance. You can’t use it without inheritance.
(it’s a subTYPE polymorphism after all) I’ll further call it just polymorphism so try not to get confused. Okay, let’s remember last lesson and start the development with creating an Animal class, which will be used as a base class for other animals. Let’s add a property, e.g. Name It will serve as an animal’s moniker in zoo. Okay, now we need to create a method that would containt Roar’s implementation for each animal, according to the given task. Let’s do this!
Access modifier – public Return type – void
Name of the method – GetRoar
It doesn’t take any arguments. Inside let’s just print some text without any specific details. Okay, i’ll leave it be and quickly create several classes for specific animals – Cat, Dog, Rat and Boozer – they all will inherit Animal Class Now i’ll go to Main method and create an array ‘animals’ of type Animal, of size 4. I’ll create an instances of every animal and put them in ‘animals’ array using the upcast. Then i’ll iterate through the array using a for loop and call GetRoar method of every object. And a Console.ReadKey() in the end. Let’s compile and look what we get here. Well, it’s of no surpise, isn’t it? All Animal inheritors have the same implemetation of GetRoar method, so it would be stupid of us to wait for a miracle to happen. Now it’s time for polymorphism to play the game. It’s important, that we can not only add ACCESS modifiers to method, we also can add specificators (qualifiers). One such specificator is ‘virtual’ Virtual gives as a great opportunity.
Image there is a method with implementation. But we always can redefine it or kinda make a new implementation any time we may need it. Let’s add this specificator to our GetRoar method. Uot tak uot. But it seems that nothing has changed. Except that specific Animal inheritors can now change the implementation We enter any class and start typing method’s name. All the same as in Animal class – method’s access modifier, return type, name and params, if there were any. The only difference is that we use the ‘override’ keyword instead of ‘virtual’, which means ‘Redefining’. And NOW we can make a new implementation for a virtual class member. “Fuuuuuuuuuuu”
Implementing GetRoar for Boozer class. And then for the other classes. Compile and look what we get. Again. Here’s what we need! We call each animal as just ‘animal’ not even knowing it’s true type
(whether it’s a cat, dog or a boozer) But thanks to polymorphism every istance of the class works according to our method redefining (overriding) Okay, for real dummies – let’s take a closer look. We create an instance of a concrete type – Boozer. Then, using type casting, we cast it to it’s parent type – Animal. this operation is called ‘upcast’ If we’re working with Animal, we don’t know what kind of instance of what class exactly is stored there. But the thing is we don’t need to know this. And, when we call GetRoar method, the overloaded method of class Boozer is called. (Because the true type of this object is Boozer). It’s important, that the base method imlpementation won’t disappear. When we create an overloaded method, we kinda ‘hide’ the old implementation with our new one. If we create a new inheritor class, and we WON’T overload the method, it’s instances will call the method of the base class. It’s also worth noticing, thath virtual class members represent it’s ‘polimorph interface’ It means, that we can overload (redefine) this interface in inheritors of this class. All in all, that’s all i wanted to tell yoy about polymorhism. We’ll soon get back to it more than once. Thanks for wathcing ExtremeCode – extreme programming channel. Subscribe, like, visit our vk group, bla-bla-bla. Goodbye.