LogIn
I don't have account.

Understanding Method Hiding and Overriding in C#

DevSniper
186 Views

In C#, method hiding and overriding are important concepts for inheritance and polymorphism. Understanding the difference between method hiding and overriding is essential for writing maintainable and efficient code in C#, especially when dealing with inheritance and polymorphism. These concepts define how methods in derived classes interact with those in base classes in different scenarios, impacting both compile-time and runtime behavior.

Method Hiding

It is all about replacing a base class method with a new implementation in the derived class using the new keyword. i.e. a derived class provides a new implementation of a method with the same signature (name and parameters) that exists in its base class. The hidden method in the base class remains accessible using a base class reference.

Method Overriding

It is all about providing a new implementation for a virtual or abstract method of a base class in a derived class. The derived method has the same signature (name and parameters) as the base class method and uses the override keyword.

Let's Explore with an Example

Assume a case, where two classes Base and Derived are defined.

Base class implementation

class Base
{
    public void f1() {
        Console.WriteLine("Base f1 called.");
    }
    public virtual void f2() {
        Console.WriteLine("Base f2 called.");
    }
    public virtual void f3() {
        Console.WriteLine("Base f3 called.");
    }
    public void f4() {
        Console.WriteLine("Base f4 called.");
    }
}
Derived class implementation
class Derived : Base
{
     public new void f1() {
        Console.WriteLine("Derived f1 called.");
    }
    public override void f2() {
        Console.WriteLine("Derived f2 called.");
    }
    public new void f3() { 
        Console.WriteLine("Derived f3 called.");
    }
    public void f5() {
        Console.WriteLine("Derived f5 called.");
    }
}

Exploring Object Creation and Method Behavior in Different Cases

1. Create Object and Reference of same class

Create Object of base class and reference also of base class and calling base class defined methods

class Program
{
    static void Main(string[] args)
    {
        Base b = new Base();
        b.f1();
        b.f2();
        b.f3();
        b.f4();
    }
}
Base f1 called.
Base f2 called.
Base f3 called.
Base f4 called.

Note :- When you call f5 in this case you will get "does not contain a definition for `f5'" error because f5 is not present in Base.

Now , Create an Object of Derived class and reference also of Derived class and calling Derived and Base (f4) class defined methods

class Program
{
    static void Main(string[] args)
    {
        Derived d = new Derived();
        d.f1();
        d.f2();
        d.f3();
        d.f4();
        d.f5();
    }
}
Derived f1 called.
Derived f2 called.
Derived f3 called.
Base f4 called.
Derived f5 called.

2. Create an Object of the Derived Class and Store it in a Base Class Reference

class Program
{
    static void Main(string[] args)
    {
        Base b = new Derived();
        b.f1();
        b.f2();
        b.f3();
        b.f4();
    }
}
Base f1 called.
Derived f2 called.
Base f3 called.
Base f4 called.

When you call f5 you will get error

class Program
{
    static void Main(string[] args)
    {
        Base b = new Derived();
        b.f5();
    }
}
main.cs(41,11): error CS1061: Type `Base' does not contain a definition for `f5' and 
no extension method `f5' of type `Base' could be found. Are you missing an assembly reference?
main.cs(5,7): (Location of the symbol related to previous error)
Compilation failed: 1 error(s), 0 warnings

3. Explicitly Cast to Derived

Create an Object of the Derived Class and Store it in a Base Class Reference and Explicitly Cast to Derived at the time of method calling

class Program
{
    static void Main(string[] args)
    {
        Base b = new Derived();
        ((Derived)b).f1();
        ((Derived)b).f2();
        ((Derived)b).f3();
        ((Derived)b).f4();
        ((Derived)b).f5();
    }
}
Derived f1 called.
Derived f2 called.
Derived f3 called.
Base f4 called.
Derived f5 called.
OR
class Program
{
    static void Main(string[] args)
    {
        Base b = new Derived();
        var d = (Derived)b;
        d.f1();
        d.f2();
        d.f3();
        d.f4();
        d.f5();
    }
}
Derived f1 called.
Derived f2 called.
Derived f3 called.
Base f4 called.
Derived f5 called.

4. Explicitly Cast to Base

Create an Object of the Derived Class and Store it in a Derived Class Reference and Explicitly Cast to Base at the time of method calling

class Program
{
    static void Main(string[] args)
    {
        Derived d = new Derived();
        var b = (Base)d;
        b.f1();
        b.f2();
        b.f3();
        b.f4();
    }
}
Base f1 called.
Derived f2 called.
Base f3 called.
Base f4 called.

Note :- When you call f5 in this case you will get "does not contain a definition for `f5'" error because f5 is not present in Base.

OR
class Program
{
    static void Main(string[] args)
    {
        Derived d = new Derived();
        ((Base)d).f1();
        ((Base)d).f2();
        ((Base)d).f3();
        ((Base)d).f4();
    }
}
Base f1 called.
Derived f2 called.
Base f3 called.
Base f4 called.