Jump to content
Search In
  • More options...
Find results that contain...
Find results in...
kaddle

C# Polymorphism explained simply?

Recommended Posts

Posted · Original PosterOP

can someone please explain polymorphism simply, and the difference between static polymorphism and dynamic polymorphism? i feel like i have a mild understanding of the concept, but it is still fairly confusing at this point.

 

from what i understand, polymorphism itself refers to when a program uses the correct method for an object even though there are other methods with the same method signature in other derived classes or the base class.

is this correct and is there more to it?

 

also why can objects be created from two different classes? for example if you have a base class named human and a derived class named male you can do:

human object = new male();

i was under the impression that when creating an object it could only be using the same class name both times like:

human object = new human();

i'm pretty sure that this has something to do with polymorphism, but the book i'm reading is not explaining this and i can't find an explanation in some videos either.


Thanks in advance.

Link to post
Share on other sites

Using your example; a Male can be declared as a Human because it is a child class and inherits all of Human's methods/attributes. The child can change what is in a method, and also call the parent class method too. We can describe polymorphism with more than one child class; lets call them Male and Female. In our program we can throw Male or Female into a calculation and it will work because both use methods and attributes from the Human class.

 

TLDR The parent class provides us a template of methods and attributes. The child classes can have their own unique behaviour in those methods/attributes, or use the parent's.

 

 

Link to post
Share on other sites

You need to take a little step back. You can't easily understand polymorphism until you understand the importance of having a certain interface (a sort of "point of contact"). You want the same logic to be able to operate on as much types of data as you can, for it to be extendable. Because you want to have that code work and serve a long time. So you don't want to change it if you don't really have to.

 

With some C# terms: Base classes (or interfaces) provide "contact points" for interacting with classes that extend/implement them via the methods that are overriden/implemented. Polymorphism allows classes to extend/implement base properties of multiple classes/interfaces as needed, though which the class may be interacted with and used as. Such as having Male and Female classes that Extend a Human class. They can both be used as the Human class. Male and Female may also implement an interface IRun (that describes a method Run), or whatever (Male and Female could then be used as IRun somewhere where logic is implemented with IRun in mind). Think multiple types of "point of contact" on the same class.

 

So you can have logic that makes the IRun "Run", and you want a Human to be able to run, or a Male and/or Female separately. If any of them implement IRun, they can then be given to that logic as IRun, to be interacted with as IRun, even though they may have completely different implementations of doing so. Same stands for the Human class. Male and Female can be interacted with as Humans in a logic that is written for Humans. That's the basics which you need to understand, there's still more to C#, generics namely, that require this to be properly understood. This is about as simple of an explanation on this as I can manage without writing an article, of which there already are many, so do what you will with this :D.

Link to post
Share on other sites

I struggled with this too. All of the well though out explanations in the world didn't help me, until I saw one, short code example. Hopefully this will help you understand some of the uses of polymorphism, and therefore to better understand polymorphism itself:

 

class Program
{
	public class Foo
	{
		// Some members here.
	}

  	public class Bar : Foo
	{
		// Some members here.
	}

	public class Baz : Bar
	{
		// Even more members here.
	}

	public class Other
	{
		// You guessed it, some members here.
	}
        
	static void Main(string[] args)
	{
		LinkedList<Foo> exampleList = new LinkedList<Foo>();
		exampleList.AddLast(new Foo()); // exampleList now contains a reference to a new Foo. There is no compiler error
		exampleList.AddLast(new Bar()); // exampleList now contains a reference to a new Bar. No compiler error because Bar is a Foo.
		exampleList.AddLast(new Baz()); // exampleList now contains a reference to a new Baz. No error because Baz is a Bar which is a Foo.
      
		// This will throw a CS1503 "Cannot convert from Other to Foo" compiler error because Other is not a child of Foo,
		// and therefore Other cannot be cast to a Foo.
		exampleList.AddLast(new Other());
	}
}

Among polymorphisms uses, the easiest to understand and most commonly occurring use is if you need to treat a bunch of different but related objects as some single type. For example, if they are all children of some class or if they all inherit some interface, as @DevBlox pointed out in his reply.


I will never succumb to the New Cult and I reject the leadership of @Aelar_Nailo and his wicked parrot armies led by @FakeCIA and @DildorTheDecent. I will keep my eyes pure and remain dedicated to the path of the One True; IlLinusNati

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now


×