Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Polymorphism | OOP Principles
course content

Зміст курсу

C# Beyond Basics

PolymorphismPolymorphism

The term Polymorphism means "occurring in several different forms". In the context of object-oriented programming, Polymorphism refers to the ability of a class to provide a common interface for its derived classes while allowing each derived class to implement specific behaviors for the inherited methods.

Normally when we are creating an object of a class, we create a variable to hold the reference of that object. And the type of the variable is usually the class of that object.

For-example:

cs

index.cs

However, we can also create a variable of a base class and store an object of a derived class in it.

For example:

cs

index.cs

The breed attribute of the Dog object is not accessible because when we store the Dog object into a Mammal variable, it implicitly gets converted into Mammal. It means that we can only access the attributes and methods that are present in the Mammal class. It doesn't mean that the value stored in the breed class is lost, it is only inaccessible and we can say that it's essentially hidden.

We can simply convert example into a Dog through explicit typecasting and the breed attribute will become available again:

cs

index.cs

However, for methods there is an additional feature called method overriding, which is somewhat similar to method overloading however we basically redefine a method from a base class in the derived class.

For example, we would want Mammal to have a method speak() such that it outputs Woof! if it's called from a Dog object, and Meow! if it is called from a Cat object:

cs

index.cs

The warnings we get in this compilation are due to overlapping names of methods in parent and child classes. In this case there is overlap between the speak methods of Cat and Mammal classes, and also between Dog and Mammal class, which is why we get two warnings.

However, notice in the end how m1, m2 and m3 execute the method defined in the Mammal class, it is because they are casted into Mammal objects.

If we want the speak method to behave the way it behaves in its original object then we can simply convert it into a virtual method. A virtual method is simply a method which allows us to override itself from derived classes in such a way that the specific implementation executed is determined at the runtime based on the type of the original object.

To make a method virtual, we simply add the virtual keyword before its return type:

cs

index.cs

To make an overridden implementation of this method, we use the keyword override before the return type in the derived class' implementation of that new implementation:

cs

index.cs

Let's now put together the two snippets and see how it affects the output:

cs

index.cs

You see that in the output, m2 and m3 use the Cat and Dog's implementation of the method because those objects were originally Cat and Dog. This whole process of overriding methods such that they behave based on their original type is called polymorphism.

One situation where this can be very useful is being able to store objects of multiple different data types into a single array or list.

For-example:

cs

index.cs

1. What two keywords are used in method overriding?
2. What is the correct way of making a method overridable?

What two keywords are used in method overriding?

Виберіть правильну відповідь

What is the correct way of making a method overridable?

Виберіть правильну відповідь

Все було зрозуміло?

Секція 5. Розділ 4
course content

Зміст курсу

C# Beyond Basics

PolymorphismPolymorphism

The term Polymorphism means "occurring in several different forms". In the context of object-oriented programming, Polymorphism refers to the ability of a class to provide a common interface for its derived classes while allowing each derived class to implement specific behaviors for the inherited methods.

Normally when we are creating an object of a class, we create a variable to hold the reference of that object. And the type of the variable is usually the class of that object.

For-example:

cs

index.cs

However, we can also create a variable of a base class and store an object of a derived class in it.

For example:

cs

index.cs

The breed attribute of the Dog object is not accessible because when we store the Dog object into a Mammal variable, it implicitly gets converted into Mammal. It means that we can only access the attributes and methods that are present in the Mammal class. It doesn't mean that the value stored in the breed class is lost, it is only inaccessible and we can say that it's essentially hidden.

We can simply convert example into a Dog through explicit typecasting and the breed attribute will become available again:

cs

index.cs

However, for methods there is an additional feature called method overriding, which is somewhat similar to method overloading however we basically redefine a method from a base class in the derived class.

For example, we would want Mammal to have a method speak() such that it outputs Woof! if it's called from a Dog object, and Meow! if it is called from a Cat object:

cs

index.cs

The warnings we get in this compilation are due to overlapping names of methods in parent and child classes. In this case there is overlap between the speak methods of Cat and Mammal classes, and also between Dog and Mammal class, which is why we get two warnings.

However, notice in the end how m1, m2 and m3 execute the method defined in the Mammal class, it is because they are casted into Mammal objects.

If we want the speak method to behave the way it behaves in its original object then we can simply convert it into a virtual method. A virtual method is simply a method which allows us to override itself from derived classes in such a way that the specific implementation executed is determined at the runtime based on the type of the original object.

To make a method virtual, we simply add the virtual keyword before its return type:

cs

index.cs

To make an overridden implementation of this method, we use the keyword override before the return type in the derived class' implementation of that new implementation:

cs

index.cs

Let's now put together the two snippets and see how it affects the output:

cs

index.cs

You see that in the output, m2 and m3 use the Cat and Dog's implementation of the method because those objects were originally Cat and Dog. This whole process of overriding methods such that they behave based on their original type is called polymorphism.

One situation where this can be very useful is being able to store objects of multiple different data types into a single array or list.

For-example:

cs

index.cs

1. What two keywords are used in method overriding?
2. What is the correct way of making a method overridable?

What two keywords are used in method overriding?

Виберіть правильну відповідь

What is the correct way of making a method overridable?

Виберіть правильну відповідь

Все було зрозуміло?

Секція 5. Розділ 4
some-alt