C# polymorphism 🎭
Ғылым және технология
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!");
}
}
}
Пікірлер: 87
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.
@figgiano1
8 ай бұрын
😂
Again! Describing difficult concepts so simply. You got the gift
Excellent example as well as simple, thank you for sharing!
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!
The best explanation of polymorphism i have ever come across. Thank you
omg i am impressed !!!!...can c# be taught so simple....thank you so much
Thanks a lot for making this video. The way you explain this is awesome
Super awesome explanation. Thank you!!!
Thank you bro.Your vids are just amazing.
Thanks for such a simple and beautiful example
i love you, my add wont allow me to watch the long tutorials otehr channels post but these are literally saving meeeeeeeeeeeeeee
Very nice and simple explanation of the subject.
Very well explained, thank you!
so simple and easy to understand. nice vido bro!
Nice and simple explanation. Thanks a lot :)
really really great! thanks
Nice and concise!
Fantastic video!
very clear explaination!
Thanks for the video Bro.
This is the first time i clicked like, subscribe and notification bell after seeing one video. (half of it) - Bro!
bro code and girafe academy is my favriot youtube channel about programming
You are an Awesome teacher🎉
Thank you!
Amazing example
excellent video!!!
Great video ! thx u save me my module 9/10 of PSI !!!! HUG FROM PORTUGAL SIUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUU
Thank you bro!
Thank you elder bro that was very helpfull 😊
hey sir ! thank you so much for making this video. Can you also make videos on encapsulation and abstraction ?
just handled an issue in my case with the help of this video, thank you so much "Bro Code"
Thanks Bro!
i have my midterms in an hour and a half. bro just saved my ass. easily the best youtuber to teach programming.
Thanks:)
Brooooooooooooooooo this is so clear!
thanks a lot man
Thanks! Explained clearly! But you missed detailing it further into compile-time and run-time polymorphism?
great to have you
This is an example of run-time polymorphism. There's also compile-time polymorphism such as method overloading.
Ah this explains why my teacher was able to hold various of values of different datatypes using the 'object' datatype as reference.
Thanks bro
🖤🖤
awesome
you are such a bro, bro.
very good video
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.
thanks
your the reallest!
nice tutorial
so nice
thanks bro
This guy is saving me so hard
Nicd
love from india sir
Gold
nice nice
Boaty McBoat Face thx for video
wow
This guy underated af
If you want to inherit from more than one class, you just use the comma "," operator?
It would be better to mention that method overriding is a run-time polymorphism first .
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 ?
@samochreno
2 жыл бұрын
yes you are correct but then you HAVE to override it not only can
@marcusaurelius3487
2 жыл бұрын
@@samochreno dont you have to override here too
@samochreno
2 жыл бұрын
@@marcusaurelius3487 i dont understand
@mariapaszkowska3311
Жыл бұрын
@@samochreno virtual method can but doesnt have to be overridden, but if the method is abstract you have to override it
@samochreno
Жыл бұрын
@@mariapaszkowska3311 i know thats what i said
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?
in line 30, why is it virtual and not abstract? especially as it's designed to be overriden
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.
bro i want to asked, where is the static and dynamic in this video?
im not a bro but still thanks for the code
Just Code bro 😉😉😉😎😎😎..
Do u have a vid on the virtual keyword
@engirckt5410
3 жыл бұрын
please make a video on it bro
@majeyork
2 жыл бұрын
Thanks bro
Random comment
A random comment down below.
as
Random
Random comment down below
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."); } } }
Your Videos are Very Helpfull, you Earned your GigaChad-Code-Logo