C# polymorphism 🎭

Поделиться
HTML-код
  • Опубликовано: 30 ноя 2024

Комментарии • 95

  • @BroCodez
    @BroCodez  3 года назад +42

    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 года назад +66

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

  • @DamnTimCan
    @DamnTimCan 2 года назад +17

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

  • @ichig085
    @ichig085 Год назад +3

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

  • @ilakutemmanuel9688
    @ilakutemmanuel9688 Год назад +1

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

  • @paleonard1979
    @paleonard1979 Год назад +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

  • @fnarmusiccomposition3418
    @fnarmusiccomposition3418 3 года назад +4

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

  • @CYON4D
    @CYON4D Год назад

    Very nice and simple explanation of the subject.

  • @raheelshayaan
    @raheelshayaan Год назад

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

  • @bartosz8117
    @bartosz8117 2 года назад +1

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

  • @MrJeeoSoft
    @MrJeeoSoft 2 года назад

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

  • @blizzardengle
    @blizzardengle 5 месяцев назад +1

    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.

    • @supertenchoo4271
      @supertenchoo4271 2 месяца назад

      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.

  • @ranjanadissanayaka5390
    @ranjanadissanayaka5390 10 месяцев назад +1

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

  • @david_eixa
    @david_eixa 2 года назад +1

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

  • @omojolajoshua
    @omojolajoshua Год назад

    Super awesome explanation. Thank you!!!

  • @tithiram
    @tithiram 7 месяцев назад

    Nice and simple explanation. Thanks a lot :)

  • @AliHassan-ec9nu
    @AliHassan-ec9nu Год назад

    Thanks for such a simple and beautiful example

  • @llamadrama169
    @llamadrama169 2 месяца назад

    Very informative, thank you.

  • @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 7 месяцев назад

    Very well explained, thank you!

  • @zoop391
    @zoop391 Год назад

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

  • @jktenny
    @jktenny Год назад

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

  • @justingiovanetti
    @justingiovanetti 10 месяцев назад

    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.

  • @GalipCanKARAARSLAN
    @GalipCanKARAARSLAN 7 месяцев назад

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

  • @kopilkaiser8991
    @kopilkaiser8991 Год назад

    You are an Awesome teacher🎉

  • @bokooo3129
    @bokooo3129 7 месяцев назад

    Thank you elder bro that was very helpfull 😊

  • @vFedora
    @vFedora 2 года назад

    Thank you bro.Your vids are just amazing.

  • @samadhimahawela1941
    @samadhimahawela1941 4 месяца назад +1

    Boaty McBoat Face

  • @hsami4208
    @hsami4208 3 года назад +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 2 года назад

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

    • @samochreno
      @samochreno 2 года назад

      @@mariapaszkowska3311 i know thats what i said

  • @H4KnSL4K
    @H4KnSL4K 5 месяцев назад

    Nice and concise!

  • @rui9078
    @rui9078 2 года назад

    so simple and easy to understand. nice vido bro!

  • @alexanderpopvelichkov8850
    @alexanderpopvelichkov8850 10 месяцев назад

    very clear explaination!

  • @sekki2554
    @sekki2554 3 года назад +6

    This guy underated af

  • @YOUTUBE_IS_WOKE
    @YOUTUBE_IS_WOKE 2 года назад

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

  • @nadaelnagar4174
    @nadaelnagar4174 Год назад

    Amazing example

  • @maxwong1768
    @maxwong1768 Год назад

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

  • @MethodOverRide
    @MethodOverRide 8 месяцев назад

    Fantastic video!

  • @hyperfitnz
    @hyperfitnz 3 месяца назад

    Legit the best explanation

  • @kvelez
    @kvelez Год назад +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.");
    }
    }
    }

  • @GeneralPet
    @GeneralPet Год назад

    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?

  • @youcan_sprules
    @youcan_sprules Год назад

    Nicd

  • @zdspider6778
    @zdspider6778 Год назад

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

  • @ahmedel-saadany703
    @ahmedel-saadany703 2 года назад

    🖤🖤

  • @najimpinjari6077
    @najimpinjari6077 Год назад

    great to have you

  • @amanda-we9fv
    @amanda-we9fv 5 месяцев назад

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

  • @Coder991
    @Coder991 Год назад

    awesome

  • @HoNow222
    @HoNow222 2 года назад

    very good video

  • @cloudguy4192
    @cloudguy4192 2 года назад

    excellent video!!!

  • @FullExtension1
    @FullExtension1 2 года назад

    you are such a bro, bro.

  • @masoudrahzani
    @masoudrahzani 2 года назад

    really really great! thanks

  • @spartanranger
    @spartanranger 3 года назад

    Thanks for the video Bro.

  • @WebDevWithProfessor
    @WebDevWithProfessor 3 месяца назад

    Great Bro!

  • @mone9366
    @mone9366 2 года назад

    Thank you!

  • @ikuubi
    @ikuubi Год назад

    Brooooooooooooooooo this is so clear!

  • @user-gq4ln6mm9j
    @user-gq4ln6mm9j 2 года назад +1

    nice nice

  • @AFaridos
    @AFaridos Год назад

    Thank you bro!

  • @adamnowicki1425
    @adamnowicki1425 Год назад

    Boaty McBoat Face
    thx for video

  • @najimpinjari6077
    @najimpinjari6077 Год назад

    love from india sir

  • @alexizz8012
    @alexizz8012 Год назад

    This guy is saving me so hard

  • @AntwiGodwill-o8y
    @AntwiGodwill-o8y 7 месяцев назад

    nice tutorial

  • @michrolaserx10
    @michrolaserx10 2 года назад

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

  •  3 года назад

    Thanks Bro!

  • @jaypatel0088
    @jaypatel0088 11 месяцев назад +1

    Just Code bro 😉😉😉😎😎😎..

  • @Pocket-domain
    @Pocket-domain Год назад

    your the reallest!

  • @qardin3079
    @qardin3079 3 года назад

    so nice

  • @alikemalulus478
    @alikemalulus478 10 месяцев назад

  • @stevancosovic4706
    @stevancosovic4706 3 года назад

    Thanks:)

  • @armanir1571
    @armanir1571 2 года назад

    thanks a lot man

  • @alexgolomb363
    @alexgolomb363 Год назад

    Thanks bro

  • @OweYaOne
    @OweYaOne Месяц назад

    too good :)

  • @gilbertdavid9582
    @gilbertdavid9582 Год назад

    thanks

  • @Tymonello
    @Tymonello 2 года назад

    thanks bro

  • @erenardakaplan7984
    @erenardakaplan7984 Год назад

    wow

  • @KarasuOfficial-g9v
    @KarasuOfficial-g9v 4 месяца назад

  • @Anthony-op7xz
    @Anthony-op7xz 3 года назад

    Do u have a vid on the virtual keyword

  • @felixconrad9248
    @felixconrad9248 2 года назад

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

  • @caseyspaulding
    @caseyspaulding Год назад

    Gold

  • @definitelynotchris4776
    @definitelynotchris4776 2 месяца назад

    lulu be like

  • @polytrave
    @polytrave Год назад

    im not a bro but still thanks for the code

  • @the_dude_josh
    @the_dude_josh 11 месяцев назад

    A random comment down below.

  • @sammoc1985
    @sammoc1985 7 месяцев назад

    Random comment

  • @ancestrall794
    @ancestrall794 2 года назад

    Random comment down below

  • @mackay2039
    @mackay2039 2 месяца назад

    random comment

  • @notyou3000
    @notyou3000 10 месяцев назад

    as

  • @weary867
    @weary867 6 месяцев назад

    Random