C# Method Overriding

Method overriding is a concept in object-oriented programming (OOP) that allows a subclass (derived class) to provide a specific implementation of a method that is already defined in its base class (parent class). This is useful when a derived class needs to modify or extend the behavior of a method that it inherits from the base class.

Rules for Method Overriding:

1. The method signature in the derived class must match the method signature in the base class.

2. The base method must be marked as virtual, abstract, or override (in case it is being overridden from another class).

3. The overridden method in the derived class should use the override keyword.

4. You cannot override a method that is marked as sealed or static.

Basic Method Overriding

Example:


using System;

class Animal {
    //Virtual method that can be overridden by derived classes
    public virtual void sound() {
        Console.WriteLine("Animals make different sounds");
    }
}

// Subclass: Dog
class Dog : Animal {
    // Overriding the sound method
    public override void sound() {
        Console.WriteLine("Dog barks");
    }
}

// Subclass: Cat
class Cat : Animal {
    // Overriding the sound method
    public override void sound() {
        Console.WriteLine("Cat meows");
    }
}

public class myProgram {
     static void Main(String[] args) {
        Animal myDog = new Dog();
        Animal myCat = new Cat();
        
        myDog.sound();  // Dog barks
        myCat.sound();  // Cat meows
    }
}

Output:

Dog barks
Cat meows

Explanation:

1. Base Class (Animal): The sound() method is defined as virtual, which means that it can be overridden in any derived class.

2. Derived Classes (Dog and Cat): Both Dog and Cat classes override the sound() method using the override keyword to provide their specific implementations of sound().

Abstract Class and Method Overriding

In this example, we use an abstract class. Abstract methods in an abstract class must be overridden by any non-abstract derived class.

Example:


using System;

abstract class Animal {
     // Abstract method
    public abstract void sound();
}

// Subclass: Dog
class Dog : Animal {
    // Overriding the sound method
    public override void sound() {
        Console.WriteLine("Dog barks");
    }
}

// Subclass: Cat
class Cat : Animal {
    // Overriding the sound method
    public override void sound() {
        Console.WriteLine("Cat meows");
    }
}

public class myProgram {
     static void Main(String[] args) {
        Animal myDog = new Dog();
        Animal myCat = new Cat();
        
        myDog.sound();  // Dog barks
        myCat.sound();  // Cat meows
    }
}

Output:

Dog barks
Cat meows

Explanation:

1. Abstract Class (Animal): The class Animal is abstract and contains an abstract method sound(), which does not have an implementation. This forces all derived classes to provide their implementation of sound().

2. Derived Classes (Dog and Cat): Both the Dog and Cat classes override the abstract method sound() to provide specific sound behavior.