• Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.
    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

Solved - C# c# polymorphism , how to think about on where to use it?

Status
This thread has been solved! Go to solution…

TheUninvited

New Coder
Jul 6, 2019
27
4
3
c#
I kinda know now what is a polymorphism but i dont have the thought on how to use it ? Like where?


what i mean is like for example:

if i want to check something i know that we use "If statement"
If i want to increment something time to time i know i should use a"for loop"

and that's where i am asking. I know some examples such as :
if you had an animal, a dog,cat could inherit from the animal and do something et.c

Like the concept i understand but the thought is what i am missing.
 

Master Yoda

Administrator
Administrator
Verified
Jan 2, 2018
1,721
432
95
Canada
codeforum.org
Perhaps you are putting "Too much" thought into it? I do the same thing, like what I think what you mean is like when and how we use it etc?

@joe, want to chip in?
 

joe

Coder
Aug 21, 2019
49
28
18
learnbuildweb.wordpress.com
As far as I understand, polymorphism is an OOP concept which allows your variables, functions or objects to behave differently in different contexts.

You would generally put it to work when you want your code to be extensible and easily scaleable. You'll come across implementation of this concept when you study source code of a fairly complex software.
 

Krusty the Senile

Active Coder
Jun 26, 2019
108
29
28
To extend your animal example, where you have an interface/base class Animal, and you have subclasses Dog and Chicken:
Say for example, the base class has a function that needs to be overridden. For this example, lets say that classes that extend or implement Animal need to implement a function getNumberOfLegs(). The Dog subclass would return 4 and the Chicken subclass would return 2.
Polymorphism becomes useful if you wanted to have a collection or array of Animal instances where you would declare that the list or array contained Animal types, and this could be either Dog or Chicken instances.
If you were to loop through the array and you know that a common characteristic of Animal instances was the function getNumberOfLegs(), you don't have to check if the object instance is either a Dog or Chicken, you know that the function getNumberOfLegs() needs to be present in both class types as they both inherit the Animal base class.
You could also potentially have a function that takes an Animal object type, in which case you could pass in a Dog or Chicken, as long as the function is dealing with functions/the interface that is defined by the Animal class.
It would probably be good to have a more concrete example ... I'll see if I can think something up.
 
  • Like
Reactions: Master Yoda

Master Yoda

Administrator
Administrator
Verified
Jan 2, 2018
1,721
432
95
Canada
codeforum.org
To extend your animal example, where you have an interface/base class Animal, and you have subclasses Dog and Chicken:
Say for example, the base class has a function that needs to be overridden. For this example, lets say that classes that extend or implement Animal need to implement a function getNumberOfLegs(). The Dog subclass would return 4 and the Chicken subclass would return 2.
Polymorphism becomes useful if you wanted to have a collection or array of Animal instances where you would declare that the list or array contained Animal types, and this could be either Dog or Chicken instances.
If you were to loop through the array and you know that a common characteristic of Animal instances was the function getNumberOfLegs(), you don't have to check if the object instance is either a Dog or Chicken, you know that the function getNumberOfLegs() needs to be present in both class types as they both inherit the Animal base class.
You could also potentially have a function that takes an Animal object type, in which case you could pass in a Dog or Chicken, as long as the function is dealing with functions/the interface that is defined by the Animal class.
It would probably be good to have a more concrete example ... I'll see if I can think something up.
Correct me if I am wrong, so with what you said.. An array is like a database?
 

Krusty the Senile

Active Coder
Jun 26, 2019
108
29
28
Correct me if I am wrong, so with what you said.. An array is like a database?
An array is more like a list. Array implementations in more recent languages probably behave more like lists these days (resizable, insert/remove elements) but in C land an array is a contiguous allocation of memory. Strings in C can also be referred to as char arrays.

... without going too much off topic, it would probably be best to replace my mention of "array" with either "list" or "collection".
 
  • Like
Reactions: Master Yoda

Bury.ai

New Coder
Jul 29, 2019
13
10
3
Hi!

Where to use polymorphism? Actually at a lot of places.

Example 1:
  • Let's say you are making a game and you will have in this scenario the following classes: Player and NPC. Now while they are two different entities can you use polymorphism here? Absolutely! You could in this situation use polymorphism have a abstract base class called Entity or IEntity interface where the class/interface would be the base class of Player/NPC.

Example 2
  • You have classes called Ferrari, Lamborgini and Porsche in this situation you can have a base class called Car or ICar.
Polymorphism really comes in handy when you want to have something like a general method which doesn't care about the specific implementation, but only about the overarching type. Using your animals example:
Code:
public static void Main()
{
    var animals = new List<Animal>();
    animals.Add(new Dog());
    animals.Add(new Cat());

    foreach (var animal in animals)
        Feed(animal);
}

public static void Feed(Animal animal)
{
    animal.Eat();
}
Note that the method doesn't care what kind of animal it gets, it's just going to try to feed it. Maybe Dog implements Eat() such that it gobbles up everything in sight. Maybe Cat() implements it such that it takes a bite and walks away. Maybe Fish() implements it such that it eats too much and dies. The method itself doesn't care which Animal it gets, and you can easily add more Animal types without having to change that method which accepts them.
 

Master Yoda

Administrator
Administrator
Verified
Jan 2, 2018
1,721
432
95
Canada
codeforum.org
Hey @TheUninvited have you found a solution to this? If so, could you post the solution if you had found it on your own or if someone here has provided you with a solution could you mark their post as the solution?
 
Status
This thread has been solved! Go to solution…