C# method overriding 🙅

preview_player
Показать описание
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
{

}
}
Рекомендации по теме
Комментарии
Автор

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
{

}
}

BroCodez
Автор

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

DivanProdOfficial
Автор

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!

marionella
Автор

Very good explanations!
Thanks for sharing.

MrSK
Автор

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.

snnyv
Автор

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

DoubleShakey
Автор

VRO thanks Short and Effective just perfect

vishwajeetpanwar
Автор

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

linusk
Автор

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

Bear-nrjo
Автор

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

cn-ml
Автор

Why does it still work without putting override and virtual

EchoVidsu