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 ?
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; 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."); } } }
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
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!
This is an example of run-time polymorphism. There's also compile-time polymorphism such as method overloading.
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
Great video ! thx u save me my module 9/10 of PSI !!!! HUG FROM PORTUGAL SIUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUU
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!
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.
Very informative, thank you.
Legit the best explanation
your the best mate, you will save with this video on my test
This guy underated af
Super awesome explanation. Thank you!!!
hey sir ! thank you so much for making this video. Can you also make videos on encapsulation and abstraction ?
i love you, my add wont allow me to watch the long tutorials otehr channels post but these are literally saving meeeeeeeeeeeeeee
Nice and simple explanation. Thanks a lot :)
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.
Thanks a lot for making this video. The way you explain this is awesome
Fantastic video!
Thanks for such a simple and beautiful example
just handled an issue in my case with the help of this video, thank you so much "Bro Code"
Amazing example
very clear explaination!
Very well explained, thank you!
Thank you bro.Your vids are just amazing.
so simple and easy to understand. nice vido bro!
You are an Awesome teacher🎉
Nice and concise!
great to have you
very good video
Great Bro!
Thank you elder bro that was very helpfull 😊
you are such a bro, bro.
Brooooooooooooooooo this is so clear!
excellent video!!!
awesome
Ah this explains why my teacher was able to hold various of values of different datatypes using the 'object' datatype as reference.
really really great! thanks
Thank you bro!
Thanks for the video Bro.
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 tutorial
Boaty McBoat Face
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.
Thank you!
in line 30, why is it virtual and not abstract? especially as it's designed to be overriden
nice nice
your the reallest!
too good :)
love from india sir
🖤🖤
This guy is saving me so hard
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?
Thanks! Explained clearly! But you missed detailing it further into compile-time and run-time polymorphism?
It would be better to mention that method overriding is a run-time polymorphism first .
thanks a lot man
Thanks bro
If you want to inherit from more than one class, you just use the comma "," operator?
Nicd
so nice
Just Code bro 😉😉😉😎😎😎..
Thanks:)
Thanks Bro!
thanks bro
Boaty McBoat Face
thx for video
bro i want to asked, where is the static and dynamic in this video?
thanks
Do u have a vid on the virtual keyword
please make a video on it bro
Thanks bro
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.");
}
}
}
wow
Gold
im not a bro but still thanks for the code
lulu be like
A random comment down below.
Your Videos are Very Helpfull, you Earned your GigaChad-Code-Logo
Random comment
as
random comment
Random
Random comment down below