C# polymorphism 🎭

preview_player
Показать описание
C# polymorphism tutorial example explained

#C# #polymorphism #tutorial

using System;

namespace MyFirstProgram
{
class Program
{
static void Main(string[] args) {

// polymorphism = Greek word that means to "have many forms"
// Objects can be identified by more than one type
// Ex. A Dog is also: Canine, Animal, Organism

Car car = new Car();
Bicycle bicycle = new Bicycle();
Boat boat = new Boat();

Vehicle[] vehicles = {car, bicycle, boat};

foreach (Vehicle vehicle in vehicles)
{
vehicle.Go();
}

Console.ReadKey();
}
}
class Vehicle
{
public virtual void Go()
{

}
}
class Car: Vehicle
{
public override void Go()
{
Console.WriteLine("The car is moving!");
}
}
class Bicycle : Vehicle
{
public override void Go()
{
Console.WriteLine("The bicycle is moving!");
}
}
class Boat : Vehicle
{
public override void Go()
{
Console.WriteLine("The boat is moving!");
}
}
}
Рекомендации по теме
Комментарии
Автор

using System;

namespace MyFirstProgram
{
class Program
{
static void Main(string[] args) {

// polymorphism = Greek word that means to "have many forms"
// Objects can be identified by more than one type
// Ex. A Dog is also: Canine, Animal, Organism

Car car = new Car();
Bicycle bicycle = new Bicycle();
Boat boat = new Boat();

Vehicle[] vehicles = {car, bicycle, boat};

foreach (Vehicle vehicle in vehicles)
{
vehicle.Go();
}

Console.ReadKey();
}
}
class Vehicle
{
public virtual void Go()
{

}
}
class Car: Vehicle
{
public override void Go()
{
Console.WriteLine("The car is moving!");
}
}
class Bicycle : Vehicle
{
public override void Go()
{
Console.WriteLine("The bicycle is moving!");
}
}
class Boat : Vehicle
{
public override void Go()
{
Console.WriteLine("The boat is moving!");
}
}
}

BroCodez
Автор

THANK YOU! Your video helped me understand more the concept better than my University teacher and three different books.

wiskasIO
Автор

Again! Describing difficult concepts so simply. You got the gift

DamnTimCan
Автор

Legend!! I've been reading a book for ages trying to understand this. Thank you for making such a simple explanation that even my brain can understand!

paleonard
Автор

This is an example of run-time polymorphism. There's also compile-time polymorphism such as method overloading.

ichig
Автор

The best explanation of polymorphism i have ever come across. Thank you

ilakutemmanuel
Автор

omg i am impressed !!!!...can c# be taught so simple....thank you so much

pavidevi
Автор

bro code and girafe academy is my favriot youtube channel about programming

fnarmusiccomposition
Автор

i have my midterms in an hour and a half. bro just saved my ass. easily the best youtuber to teach programming.

raheelshayaan
Автор

Excellent example as well as simple, thank you for sharing!

MrJeeoSoft
Автор

Great tutorial! You should use abstract instead of virtual for the Go method in this case though. This would also mean the class Vehicle has to be abstract as well.

blizzardengle
Автор

hey sir ! thank you so much for making this video. Can you also make videos on encapsulation and abstraction ?

ranjanadissanayaka
Автор

This is the first time i clicked like, subscribe and notification bell after seeing one video. (half of it) - Bro!

bartosz
Автор

i love you, my add wont allow me to watch the long tutorials otehr channels post but these are literally saving

YEAR
Автор

Thanks a lot for making this video. The way you explain this is awesome

jktenny
Автор

Thanks for such a simple and beautiful example

AliHassan-ecnu
Автор

Super awesome explanation. Thank you!!!

omojolajoshua
Автор

Nice and simple explanation. Thanks a lot :)

tithiram
Автор

Great video ! thx u save me my module 9/10 of PSI !!!! HUG FROM PORTUGAL

davidsantos
Автор

Very nice and simple explanation of the subject.

CYOND