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 { } }
@DivanProdOfficial3 жыл бұрын
so short and effective!! thank you for not wasting my time!
@marionella1752 жыл бұрын
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!
@valenzsa418 Жыл бұрын
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.
@DoubleShakey2 жыл бұрын
bro,.. great videos, so simple and straight to the point. good review!
@muhammadtaha42086 күн бұрын
Explained it way better in 4 minutes than what my teacher couldnt in 4 months
@abdelrahmangomaaabbady Жыл бұрын
Wonderful tutorial, great thanks.
@MrSK168 Жыл бұрын
Very good explanations! Thanks for sharing.
@nikunjsethi935111 ай бұрын
Thanks for the explanation
@thisdeath4 ай бұрын
3:30 reminded me of gin ibushi hahaa but nice video 😁👍
@linus3k Жыл бұрын
very good vid, to the point and very good explanation.
@raquelmarino46163 ай бұрын
bro is always saving me for my assignments in the shortest amount of time
@badrbennani38012 жыл бұрын
very helpful video. thanks
@goverdhanjayaram3683 Жыл бұрын
Super effective !! Thank you !
@vishwajeetpanwar32713 жыл бұрын
VRO thanks Short and Effective just perfect
@qnghost253 Жыл бұрын
Simple and helpful
@adambosick313710 ай бұрын
Keep the content coming.
@FullExtension12 жыл бұрын
you are such a bro, bro.
@JugalSingh Жыл бұрын
great video
@Bear-nr9jo3 жыл бұрын
Great vid bro, you helped me understand c# :D
@sainikhithakadam49398 ай бұрын
Thank you
@spartanranger3 жыл бұрын
Thanks for the video Bro.
@TheMursolАй бұрын
Thank you bro !
@johneconomou5672 жыл бұрын
broooooooooo amazing
@liberjesus93755 ай бұрын
This is that good that I saved around 100 lines of code
@Dadax93983 жыл бұрын
The thumbnails always get me x)
@amalamel6727 Жыл бұрын
Thanks
@mertkurt947 Жыл бұрын
yea maaaaaaaaan , super helpful
@milorday5141 Жыл бұрын
thank you
@sametayaz4891 Жыл бұрын
what does the fox say?
@Eldritch_8 ай бұрын
a random comment down below
@pajkeav2 жыл бұрын
nice
@FaizyPlayz786 Жыл бұрын
❣
@triton54572 жыл бұрын
good vid bro
@cn-ml3 жыл бұрын
My compiler says the derived class has to use either override or new? What is new used for in this context?
@MSport0304 ай бұрын
What does the catdog go?
3 жыл бұрын
Thanks Bro!
@AmanSharma-hg2wy Жыл бұрын
❤
@courtneywoodcock56962 жыл бұрын
thanks
@definitelynotchris47763 ай бұрын
woof
@BenBerke2 жыл бұрын
so whats the point of overriding? Why not just create a new method that just prints a line?
@thendar8232 жыл бұрын
Ill put my comment here so in case someone smarter than me comes along and gives an explanation, I can read it too
@RalphBrandsma2 жыл бұрын
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!"); } } }
@whitedinamo3 жыл бұрын
lesson check😇
@abhinay4200 Жыл бұрын
What about new Keyword what happens when parent method is virtual and child is new
@EchoVids2u3 жыл бұрын
Why does it still work without putting override and virtual
@nolle2 жыл бұрын
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
@abdesslemebenhassine42508 ай бұрын
it is useless here u could type it directly without virtual , override right ?
@Дмитрий-п9ь7щ6 ай бұрын
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