when you have a derive class (child class) and base class (parent classs). You can implement the base class (parent class) methods in derived class (child class). In base class we use virtual keyword for defining the method. We use override keyword for implementing the base class method in derive class. Overriding is feature of Object Oriented Programming .
when you have a derive class (child class) and base class (parent classs). You can implement the base class (parent class) methods in derived class (child class). In base class we use virtual keyword for defining the method.
We use override keyword for implementing the base class method in derive class. It is feature of Object Oriented Programming.
In above diagram we have Fruit class Which is parent class of two child classes like Mango and WaterMelon. The Fruit class has one virtual method Taste() that is overridden in two derived classes Mango and WaterMleon.
Here, bleow code is an example of method overriding, We have Fruit class that have Taste virtual method.
We have inherited this Fruit class with WaterMelon subclass class. So in WaterMelon subclass Class Taste method is overriding. We have inherited this Fruit class with Mango subclass class. So in Mango subclass Class Taste method is overriding.
// Base class (parent class)
// Derive class (child class)
// Derive class (child class)
// Base class (parent class)
// Derive class (child class)
// Derive class (child class)
Here's a basic breakdown of how Fruit class is working in below exmaple: | |
Taste() | Taste() is an vistual method of Fruit base class. |
Here's a basic breakdown of how WaterMelon subclass is working in below exmaple: | |
Taste() | Taste() is an override method of Fruit base class but has characteristics in WaterMelon subclass also. |
Here's a basic breakdown of how Mango subclass is working in below exmaple: | |
Taste() | Taste() is an override method of Fruit base class but has characteristics in Mango subclass also. |
Here, bleow code is an example of method overriding, We have Fruit class that have Taste virtual method.
We have inherited this Fruit class with WaterMelon subclass class. So in WaterMelon subclass Class Taste method is overriding. We have inherited this Fruit class also with Mango subclass class. So in Mango subclass Class Taste method is overriding.
// Base class (parent class)
// Derive class (child class)
// Derive class (child class)
// Base class (parent class)
// Derive class (child class)
// Derive class (child class)
In method Overriding you can implement the base class behavior in derived classs. This flexibility and polymorphism make it a powerful feature for designing object-oriented programs in C#.