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!"); } } }
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!
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.
That's right to force each method to Implement go you should make vehicle as abstract cos using normal class with virtual it will be an option to be implemented in child classes, but go must be an abstract for each class inherit vehicle to be force to implement it.
Thankyou for this, correct me if I’m wrong but can you also have vehicle as as abstract class and have a abstract method with no body which can then be overridden by the other classes ?
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApp1 { internal class Program { static void Main(string[] args) { Car car = new Car(); Truck truck = new Truck(); Vehicle[] vehicles = {car, truck}; List transportation = new List(); transportation.Add(car); transportation.Add(truck); foreach (var transport in transportation) { transport.Go(); } foreach (var vehicle in vehicles) { vehicle.Go(); } } } abstract class Vehicle { abstract public int Speed { get; set; } abstract public int Wheels { get; set; } abstract public void Go(); } class Car : Vehicle { public override int Speed { get; set; } public override int Wheels { get; set;} public override void Go() { Console.WriteLine("Car is Moving..."); } } class Truck : Car { public override void Go() { Console.WriteLine("Truck is motion."); } } }
Ok but how does it actually work? If it’s like casting where each object is cast to their base class then wouldn’t they lose all their properties that were defined in the child class but not the base?
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!");
}
}
}
THANK YOU! Your video helped me understand more the concept better than my University teacher and three different books.
😂
Again! Describing difficult concepts so simply. You got the gift
This is an example of run-time polymorphism. There's also compile-time polymorphism such as method overloading.
The best explanation of polymorphism i have ever come across. Thank you
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!
omg i am impressed !!!!...can c# be taught so simple....thank you so much
bro code and girafe academy is my favriot youtube channel about programming
Very nice and simple explanation of the subject.
i have my midterms in an hour and a half. bro just saved my ass. easily the best youtuber to teach programming.
This is the first time i clicked like, subscribe and notification bell after seeing one video. (half of it) - Bro!
Excellent example as well as simple, thank you for sharing!
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.
That's right to force each method to Implement go you should make vehicle as abstract cos using normal class with virtual it will be an option to be implemented in child classes, but go must be an abstract for each class inherit vehicle to be force to implement it.
hey sir ! thank you so much for making this video. Can you also make videos on encapsulation and abstraction ?
Great video ! thx u save me my module 9/10 of PSI !!!! HUG FROM PORTUGAL SIUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUU
Super awesome explanation. Thank you!!!
Nice and simple explanation. Thanks a lot :)
Thanks for such a simple and beautiful example
Very informative, thank you.
i love you, my add wont allow me to watch the long tutorials otehr channels post but these are literally saving meeeeeeeeeeeeeee
Very well explained, thank you!
Ah this explains why my teacher was able to hold various of values of different datatypes using the 'object' datatype as reference.
Thanks a lot for making this video. The way you explain this is awesome
My instincts when hearing “polymorphism” is to think cat changes into dog. Car changes to boat. Not cat is also an animal, or car is also a vehicle.
just handled an issue in my case with the help of this video, thank you so much "Bro Code"
You are an Awesome teacher🎉
Thank you elder bro that was very helpfull 😊
Thank you bro.Your vids are just amazing.
Boaty McBoat Face
Thankyou for this, correct me if I’m wrong but can you also have vehicle as as abstract class and have a abstract method with no body which can then be overridden by the other classes ?
yes you are correct but then you HAVE to override it not only can
@@samochreno dont you have to override here too
@@marcusaurelius3487 i dont understand
@@samochreno virtual method can but doesnt have to be overridden, but if the method is abstract you have to override it
@@mariapaszkowska3311 i know thats what i said
Nice and concise!
so simple and easy to understand. nice vido bro!
very clear explaination!
This guy underated af
Thanks! Explained clearly! But you missed detailing it further into compile-time and run-time polymorphism?
Amazing example
It would be better to mention that method overriding is a run-time polymorphism first .
Fantastic video!
Legit the best explanation
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
internal class Program
{
static void Main(string[] args)
{
Car car = new Car();
Truck truck = new Truck();
Vehicle[] vehicles = {car, truck};
List transportation = new List();
transportation.Add(car);
transportation.Add(truck);
foreach (var transport in transportation)
{
transport.Go();
}
foreach (var vehicle in vehicles)
{
vehicle.Go();
}
}
}
abstract class Vehicle
{
abstract public int Speed { get; set; }
abstract public int Wheels { get; set; }
abstract public void Go();
}
class Car : Vehicle
{
public override int Speed { get; set; }
public override int Wheels { get; set;}
public override void Go()
{
Console.WriteLine("Car is Moving...");
}
}
class Truck : Car
{
public override void Go()
{
Console.WriteLine("Truck is motion.");
}
}
}
Ok but how does it actually work? If it’s like casting where each object is cast to their base class then wouldn’t they lose all their properties that were defined in the child class but not the base?
Nicd
If you want to inherit from more than one class, you just use the comma "," operator?
🖤🖤
great to have you
in line 30, why is it virtual and not abstract? especially as it's designed to be overriden
awesome
very good video
excellent video!!!
you are such a bro, bro.
really really great! thanks
Thanks for the video Bro.
Great Bro!
Thank you!
Brooooooooooooooooo this is so clear!
nice nice
Thank you bro!
Boaty McBoat Face
thx for video
love from india sir
This guy is saving me so hard
nice tutorial
bro i want to asked, where is the static and dynamic in this video?
Thanks Bro!
Just Code bro 😉😉😉😎😎😎..
your the reallest!
so nice
Thanks:)
thanks a lot man
Thanks bro
too good :)
thanks
thanks bro
wow
Do u have a vid on the virtual keyword
please make a video on it bro
Thanks bro
Your Videos are Very Helpfull, you Earned your GigaChad-Code-Logo
Gold
lulu be like
im not a bro but still thanks for the code
A random comment down below.
Random comment
Random comment down below
random comment
as
Random