C# polymorphism 🎭

Поділитися
Вставка
  • Опубліковано 2 лип 2021
  • 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

  • @BroCodez
    @BroCodez  3 роки тому +40

    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!");
    }
    }
    }

  • @wiskasIO
    @wiskasIO 2 роки тому +57

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

  • @videolosss
    @videolosss 2 роки тому +12

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

  • @MrJeeoSoft
    @MrJeeoSoft 2 роки тому

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

  • @ilakutemmanuel9688
    @ilakutemmanuel9688 10 місяців тому +1

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

  • @paleonard1979
    @paleonard1979 8 місяців тому +1

    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!

  • @pavidevi86
    @pavidevi86 2 роки тому +3

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

  • @CYON4D
    @CYON4D Рік тому

    Very nice and simple explanation of the subject.

  • @omojolajoshua
    @omojolajoshua Рік тому

    Super awesome explanation. Thank you!!!

  • @jktenny
    @jktenny Рік тому

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

  • @AliHassan-ec9nu
    @AliHassan-ec9nu Рік тому

    Thanks for such a simple and beautiful example

  • @MethodOverRide
    @MethodOverRide 4 місяці тому

    Fantastic video!

  • @vFedora
    @vFedora 2 роки тому

    Thank you bro.Your vids are just amazing.

  • @tithiram
    @tithiram 2 місяці тому

    Nice and simple explanation. Thanks a lot :)

  • @rui9078
    @rui9078 2 роки тому

    so simple and easy to understand. nice vido bro!

  • @YEAR2001
    @YEAR2001 2 роки тому

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

  • @alex_opr
    @alex_opr 2 місяці тому

    Very well explained, thank you!

  • @H4KnSL4K
    @H4KnSL4K Місяць тому

    Nice and concise!

  • @ranjanadissanayaka5390
    @ranjanadissanayaka5390 6 місяців тому +1

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

  • @bartosz8117
    @bartosz8117 Рік тому +1

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

  • @alexanderpopvelichkov8850
    @alexanderpopvelichkov8850 6 місяців тому

    very clear explaination!

  • @bokooo3129
    @bokooo3129 3 місяці тому

    Thank you elder bro that was very helpfull 😊

  • @STUPIDYOUTUBE_HIDINGMSGS
    @STUPIDYOUTUBE_HIDINGMSGS 2 роки тому

    Thanks! Explained clearly! But you missed detailing it further into compile-time and run-time polymorphism?

  • @spartanranger
    @spartanranger 2 роки тому

    Thanks for the video Bro.

  • @fnarmusiccomposition3418
    @fnarmusiccomposition3418 2 роки тому +4

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

  • @masoudrahzani
    @masoudrahzani 2 роки тому

    really really great! thanks

  • @kopilkaiser8991
    @kopilkaiser8991 Рік тому

    You are an Awesome teacher🎉

  • @raheelshayaan
    @raheelshayaan 9 місяців тому

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

  • @ichig085
    @ichig085 9 місяців тому +1

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

  • @cloudguy4192
    @cloudguy4192 Рік тому

    excellent video!!!

  • @blizzardengle
    @blizzardengle Місяць тому

    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.

  • @user-pc2vv6ue8v
    @user-pc2vv6ue8v 3 місяці тому

    just handled an issue in my case with the help of this video, thank you so much "Bro Code"

  • @nadaelnagar4174
    @nadaelnagar4174 10 місяців тому

    Amazing example

  • @Coder991
    @Coder991 Рік тому

    awesome

  • @davidsantos8249
    @davidsantos8249 2 роки тому +1

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

  • @najimpinjari6077
    @najimpinjari6077 8 місяців тому

    great to have you

  • @amanda-we9fv
    @amanda-we9fv Місяць тому

    in line 30, why is it virtual and not abstract? especially as it's designed to be overriden

  • @ikuubi
    @ikuubi 8 місяців тому

    Brooooooooooooooooo this is so clear!

  • @GeneralPet
    @GeneralPet 8 місяців тому

    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?

  • @AFaridos
    @AFaridos Рік тому

    Thank you bro!

  • @mone9366
    @mone9366 Рік тому

    Thank you!

  • @HoNow222
    @HoNow222 Рік тому

    very good video

  • @user-so6pp9iq5y
    @user-so6pp9iq5y 2 місяці тому

    nice tutorial

  • @ahmedel-saadany703
    @ahmedel-saadany703 Рік тому

    🖤🖤

  • @FullExtension1
    @FullExtension1 Рік тому

    you are such a bro, bro.

  • @alexgolomb363
    @alexgolomb363 Рік тому

    Thanks bro

  • @michrolaserx10
    @michrolaserx10 2 роки тому

    bro i want to asked, where is the static and dynamic in this video?

  •  3 роки тому

    Thanks Bro!

  • @youcan_sprules
    @youcan_sprules Рік тому

    Nicd

  • @zdspider6778
    @zdspider6778 Рік тому

    If you want to inherit from more than one class, you just use the comma "," operator?

  • @stevancosovic4706
    @stevancosovic4706 3 роки тому

    Thanks:)

  • @zoop391
    @zoop391 Рік тому

    Ah this explains why my teacher was able to hold various of values of different datatypes using the 'object' datatype as reference.

  • @dimitryaujour318
    @dimitryaujour318 Рік тому

    your the reallest!

  • @armanir1571
    @armanir1571 Рік тому

    thanks a lot man

  • @qardin3079
    @qardin3079 2 роки тому

    so nice

  • @gilbertdavid9582
    @gilbertdavid9582 Рік тому

    thanks

  • @najimpinjari6077
    @najimpinjari6077 8 місяців тому

    love from india sir

  • @adamnowicki1425
    @adamnowicki1425 Рік тому

    Boaty McBoat Face
    thx for video

  • @erenardakaplan7984
    @erenardakaplan7984 7 місяців тому

    wow

  • @Tymonello
    @Tymonello Рік тому

    thanks bro

  • @user-gq4ln6mm9j
    @user-gq4ln6mm9j Рік тому +1

    nice nice

  • @hsami4208
    @hsami4208 2 роки тому +3

    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
      @samochreno 2 роки тому

      yes you are correct but then you HAVE to override it not only can

    • @marcusaurelius3487
      @marcusaurelius3487 2 роки тому

      @@samochreno dont you have to override here too

    • @samochreno
      @samochreno 2 роки тому

      @@marcusaurelius3487 i dont understand

    • @mariapaszkowska3311
      @mariapaszkowska3311 Рік тому

      @@samochreno virtual method can but doesnt have to be overridden, but if the method is abstract you have to override it

    • @samochreno
      @samochreno Рік тому

      @@mariapaszkowska3311 i know thats what i said

  • @caseyspaulding
    @caseyspaulding Рік тому

    Gold

  • @alexizz8012
    @alexizz8012 Рік тому

    This guy is saving me so hard

  • @sekki2554
    @sekki2554 2 роки тому +6

    This guy underated af

  • @maxwong1768
    @maxwong1768 Рік тому

    It would be better to mention that method overriding is a run-time polymorphism first .

  • @justingiovanetti
    @justingiovanetti 5 місяців тому

    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.

  • @alikemalulus478
    @alikemalulus478 6 місяців тому

  • @polytrave
    @polytrave 8 місяців тому

    im not a bro but still thanks for the code

  • @Anthony-op7xz
    @Anthony-op7xz 3 роки тому

    Do u have a vid on the virtual keyword

  • @MustafaKaradeniz-yy1cx
    @MustafaKaradeniz-yy1cx 11 днів тому

  • @jaypatel0088
    @jaypatel0088 6 місяців тому +1

    Just Code bro 😉😉😉😎😎😎..

  • @the_dude_josh
    @the_dude_josh 6 місяців тому

    A random comment down below.

  • @notyou3000
    @notyou3000 6 місяців тому

    as

  • @sammoc1985
    @sammoc1985 2 місяці тому

    Random comment

  • @ancestrall794
    @ancestrall794 Рік тому

    Random comment down below

  • @weary867
    @weary867 2 місяці тому

    Random

  • @kvelez
    @kvelez 9 місяців тому +1

    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.");
    }
    }
    }

  • @felixconrad9248
    @felixconrad9248 2 роки тому

    Your Videos are Very Helpfull, you Earned your GigaChad-Code-Logo