C# method overriding 🙅

Поделиться
HTML-код
  • Опубликовано: 19 окт 2024
  • C# method overriding tutorial example explained
    #C# #method #override
    using System;
    namespace MyFirstProgram
    {
    class Program
    {
    static void Main(string[] args) {
    //method overriding = provides a new version of a method inherited from a parent class
    // inherited method must be: abstract, virtual, or already overriden
    // Used with ToString(), polymorphism
    Dog dog = new Dog();
    Cat cat = new Cat();
    dog.Speak();
    cat.Speak();
    Console.ReadKey();
    }
    }
    class Animal
    {
    public virtual void Speak()
    {
    Console.WriteLine("The animal goes *brrr*");
    }
    }
    class Dog : Animal
    {
    public override void Speak()
    {
    Console.WriteLine("The dog goes *woof*");
    }
    }
    class Cat : Animal
    {
    }
    }

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

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

    using System;
    namespace MyFirstProgram
    {
    class Program
    {
    static void Main(string[] args) {
    //method overriding = provides a new version of a method inherited from a parent class
    // inherited method must be: abstract, virtual, or already overriden
    // Used with ToString(), polymorphism
    Dog dog = new Dog();
    Cat cat = new Cat();
    dog.Speak();
    cat.Speak();
    Console.ReadKey();
    }
    }
    class Animal
    {
    public virtual void Speak()
    {
    Console.WriteLine("The animal goes *brrr*");
    }
    }
    class Dog : Animal
    {
    public override void Speak()
    {
    Console.WriteLine("The dog goes *woof*");
    }
    }
    class Cat : Animal
    {
    }
    }

  • @DivanProdOfficial
    @DivanProdOfficial 2 года назад +35

    so short and effective!! thank you for not wasting my time!

  • @marionella175
    @marionella175 2 года назад +4

    Wow! I've spent more than 15 minutes listening to a video on my C# course about it, I almost fell asleep and absolutely didn't get how method overriding works, BUT 2 minutes of your video and I get it so clear. Thank you very much!

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

    Thank you! This clarifies my confusion as to when and the difference that a method can be overriden if whether its from a regular class or abstract class.

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

    bro,.. great videos, so simple and straight to the point. good review!

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

    Very good explanations!
    Thanks for sharing.

  • @abdelrahmangomaaabbady
    @abdelrahmangomaaabbady 9 месяцев назад

    Wonderful tutorial, great thanks.

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

    Thanks for the explanation

  • @raquelmarino4616
    @raquelmarino4616 26 дней назад

    bro is always saving me for my assignments in the shortest amount of time

  • @goverdhanjayaram3683
    @goverdhanjayaram3683 9 месяцев назад

    Super effective !! Thank you !

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

    VRO thanks Short and Effective just perfect

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

    3:30 reminded me of gin ibushi hahaa
    but nice video 😁👍

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

    very helpful video. thanks

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

    very good vid, to the point and very good explanation.

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

    Thank you

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

    Simple and helpful

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

    you are such a bro, bro.

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

    This is that good that I saved around 100 lines of code

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

    Keep the content coming.

  • @Bear-nr9jo
    @Bear-nr9jo 3 года назад +2

    Great vid bro, you helped me understand c# :D

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

    Thanks for the video Bro.

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

    great video

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

    The thumbnails always get me x)

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

    Thanks

  • @LuizHenrique-gm1cc
    @LuizHenrique-gm1cc Год назад

    Thanks bro!

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

    yea maaaaaaaaan , super helpful

  • @פרחיהיקינטון
    @פרחיהיקינטון 7 месяцев назад

    thanks

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

  • @AmanSharma-hg2wy
    @AmanSharma-hg2wy Год назад

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

    good vid bro

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

    broooooooooo amazing

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

    nice

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

    What does the catdog go?

  • @cn-ml
    @cn-ml 2 года назад

    My compiler says the derived class has to use either override or new? What is new used for in this context?

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

    What about new Keyword what happens when parent method is virtual and child is new

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

    what does the fox say?

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

    lesson check😇

  • @definitelynotchris4776
    @definitelynotchris4776 18 дней назад

    woof

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

    But what does the fox say?

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

    so whats the point of overriding? Why not just create a new method that just prints a line?

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

      Ill put my comment here so in case someone smarter than me comes along and gives an explanation, I can read it too

    • @RalphBrandsma
      @RalphBrandsma 2 года назад +4

      The point of overriding in this case is that with his structure all animals that inherit from the base class have a method Speak() which creates uniformity. when writing the base class, an animal that is not specific makes a **brr** noise.
      A possibly better example is if you have a game, for instance,
      with a base class: Sword that has the method Attack() that has a 20% chance to hit,
      and an inherited class: MagicSword.
      with this setup all Swords and MagicSwords can have the method Attack() called,
      but what if when attacking, the magic sword always hits because *magic*.
      well that is a good use for overrides as you can override the Sword implementation of Attack() and you would still be able to treat it as a sword as shown below.
      If you instead had individual Attack() methods, then the base class cannot have a method named Attack(), so any generic swords would be unable to attack.
      **EXAMPLE**
      using System;
      namespace Example
      {
      class Program
      {
      static void Main(string[] args)
      {
      List mySwords = new List();
      mySwords.Add(new Sword());
      mySwords.Add(new MagicSword());
      foreach (var sword in mySwords)
      {
      sword.Attack();
      }
      Console.ReadKey();
      }
      }
      class Sword
      {
      public virtual void Attack()
      {
      Random random = new Random();
      float chanceToHit = random.NextSingle();
      if (chanceToHit > 0.2f)
      {
      Console.WriteLine("Success!");
      }
      else
      {
      Console.WriteLine("You Missed!");
      }
      }
      }
      class MagicSword : Sword
      {
      public override void Attack()
      {
      Console.WriteLine("Success!");
      }
      }
      }

  • @Eldritch_
    @Eldritch_ 5 месяцев назад +2

    a random comment down below

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

    Why does it still work without putting override and virtual

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

      my guess is that you create a new method (with the same name) within that object, which is still valid and will work but it's not a good idea

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

    Random Comment 🤠👍

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

    it is useless here u could type it directly without virtual , override right ?

    • @Дмитрий-п9ь7щ
      @Дмитрий-п9ь7щ 3 месяца назад

      Not useless. For example, you need a method for many objects, that derive from one class. But SOME of these objects are exceptions and each of them needs a different method

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

    🗿🗿🗿🗿🗿 Done!

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

    1

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

    A random comment down below.

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

    Random Comment Down Below!!!

  • @milorday5141
    @milorday5141 9 месяцев назад

    thank you

  •  3 года назад

    Thanks Bro!

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

    thanks