Run-Time Polymorphism in C#. Master the Polymorphism concept | by Rikam Palkar | May, 2022

Grasp the Polymorphism idea

Photograph by Farzad Nazifi on Unsplash

Within the final article, we noticed what in God’s title Method Overloading is and the way we are able to obtain it. Technique overriding is the long-lost sibling of methodology overloading, however there may be one elementary distinction that units them aside.

That is half 2 of the Polymorphism sequence, the place we’re going to take a deep dive into polymorphism. The next are the three items of the puzzle.

  1. Compile-time polymorphism
  2. Run-time polymorphism
  3. Technique hiding/ shadowing

We are going to evaluate overriding with overloading which is not going to solely clear the confusion between these two but additionally give a greater understanding of overriding.

Overloading means having a number of conduct the place overriding means altering the conduct.

Following are the important thing variations between overloading and overriding.

  1. In methodology overloading, we now have to vary the signature, whereas in methodology overriding we now have to maintain the identical methodology signature together with return-type and entry specifier, it’s a strategy of reusability. That means, redefining the mother or father class methodology within the baby class with precisely the identical signature.
  2. Strategies could be overloaded throughout the similar class and inside subclasses as nicely. Whereas overriding strategies can solely be overridden in a subclass, in case you attempt to override a technique throughout the similar class utilizing the identical signature, you’re going to get a compile-time exception proven under.

The Exception: Sort ‘class title’ already defines a member known as ‘methodology title’ with the identical parameter varieties.

Crucial rule: You possibly can solely override digital or summary strategies, which suggests the mother or father class wants to embellish a technique with ‘digital’ or ‘summary’ key phrases that can enable the kid class to override. You can’t override strategies that aren’t digital or summary.

Let’s begin coding!

Rule number one: Technique signature ought to match together with return-type and entry specifier.

Itemizing 1: Overridden methodology GetOS()

Let’s name these strategies with their sort to see if this labored?

As you possibly can see within the image-1 under, an occasion of mother or father class SmartPhone is asking its model of GetOS() and printing message “Android” whereas, an occasion of kid class Iphone is asking its model of GetOS() and printing message “iOS”. It’s because we’ve efficiently overridden a technique GetOS() in class IPhone.

Picture 1: Mum or dad and baby courses objects calling their respective GetOS() methodology

Word: If you happen to didn’t outline a technique in baby class then the kid class object will name the mother or father’s methodology. If you happen to had code that appears like itemizing 2, you then’ll get output that appears like image-2.

Itemizing 2: Technique GetOS() is just not overridden in baby class

Within the following picture 2, you possibly can see each mother or father and baby class objects are calling mother or father class model of GetOs()

Picture 2: Mum or dad and baby courses objects calling mother or father’s GetOS() methodology

Let’s see just a few false situations down the road, that are vital to know!!

False state of affairs 1: what occurs if I attempt to override a technique with a distinct return sort?

I get the next exception!

The Exception: ‘IPhone.GetOS()’: return sort should be ‘string’ to match overridden member ‘SmartPhone.GetOS()’

Picture 3: Compile time exception for various return sort

False state of affairs 2: what occurs if I attempt to override a technique with a distinct entry specifier?

I get the next exception!

The Exception: ‘IPhone.GetOS()’: return sort should be ‘string’ to match overridden member ‘SmartPhone.GetOS()’

Picture 4: Compile time exception for various entry specifier

False state of affairs 3: what occurs if I attempt to override a technique which isn’t digital or summary?

I get the next exception in fact!

The Exception: ‘IPhone.GetOS()’: return sort should be ‘string’ to match overridden member ‘SmartPhone.GetOS()’

Picture 5: Compile time exception thrown whereas overriding a non-virtual methodology

Right here you may argue that I can merely skip the override key phrase within the baby class, sure you possibly can however then it gained’t be an overridden methodology. It will likely be an impartial methodology that belongs to sort ‘IPhone’.

And that is known as methodology hiding when mother or father and baby courses have the identical methodology title with the identical signature with out ‘digital’ or ‘summary’ key phrases then the kid class hides the implementation of the mother or father’s class methodology.

Let’s take another state of affairs, this was one of many interview questions as nicely. What if in case you have a multilevel inheritance? One thing like itemizing 3:

Itemizing 3 : SmartPhone <- IPhone <- IphoneX

New grandchild class IphoneX has its personal model of GetOS(), So with the article of IPhoneX we get its model of GetOS().

Picture 6: Grandchild is asking its personal implementation of GetOS()

The true query is, what if grandchild IPhoneX doesn’t have a model of the overridden methodology? so with its object, will it seek advice from mother or father’s IPhone’s GetOS() or grandparent’s SmartPhone’s GetOS()? Let’s discover out.

Itemizing 4 attracts this state of affairs.

Itemizing 4: Grandchild lacking overridden methodology
Picture 7: Grandchild refers back to the rapid overridden methodology

Keep in mind this hierarchy of preferences : Native -> rapid mother or father -> their mother or father -> until the bottom class.

If you happen to take a look at that, the grandchild finally ends up calling its mother or father’s model of GetOS(). It’s because the grandchild first regarded in itself if the overridden methodology exists. If not then it goes to the mother or father to see if a model of overridden methodology exists. Because it did exist within the mother or father it stopped its search and accessed the tactic.

OK!! Now we all know that it accesses the rapid methodology within the case of override, however what occurs when the tactic is digital? Let’s see.

Think about the next state of affairs:

Itemizing 5: Grandchild has entry to 2 digital strategies

Right here additionally the identical rule applies, it seems to be for the rapid mother or father and tries to resolve the matter. Within the following picture, you possibly can see IPhoneX can also be printing “iOS” which is its mother or father’s model of GetOS().

Picture 8: Grandchild refers back to the rapid digital methodology mother or father’s

Only for enjoyable let’s delete the mother or father model of GetOS() as nicely.

Itemizing 6: Grandchild has entry to GrandParent’s digital methodology
Picture 9: Grandchild refers back to the rapid digital methodology which is GrandParent’s

Now that it has reached the bottom class SmartPhone and it did discover the tactic with an identical signature, it stopped its searched and printed the output “Android”.

More Posts