Showing posts with label Polymorphism. Show all posts
Showing posts with label Polymorphism. Show all posts

Monday, 17 November 2025

Method Hiding in C#

Method Hiding in C#

Method Hiding

Method hiding occurs when a derived class defines a method with the same name as a method in the base class but uses the new keyword. The version of the method called depends on the reference type.

C# Example:

using System; class Car { public void Start() { Console.WriteLine("Car is starting."); } } class SportsCar : Car { public new void Start() { Console.WriteLine("SportsCar is starting with turbo!"); } } class Program { static void Main() { Car carRef = new Car(); carRef.Start(); SportsCar sportsCarRef = new SportsCar(); sportsCarRef.Start(); Car baseRef = new SportsCar(); baseRef.Start(); } }

Output:

Car is starting. SportsCar is starting with turbo! Car is starting.

Function Overriding Example in C#

This C# tutorial demonstrates runtime polymorphism using the virtual and override keywords with a Car and SportsCar class structure.

C# Function Overriding Code Example

using System;

class Car
{
    public virtual void Start() => Console.WriteLine("Car is starting.");
}

class SportsCar : Car
{
    public override void Start() => Console.WriteLine("SportsCar is starting with turbo!");
}

class Program
{
    static void Main()
    {
        new Car().Start();
        new SportsCar().Start();
        Car carRef = new SportsCar();
        carRef.Start();
    }
}

Console Output

Car is starting.
SportsCar is starting with turbo!
SportsCar is starting with turbo!

How This Example Works (Step-by-Step)

  • Virtual & Override: The base Car.Start() is marked virtual, allowing SportsCar to replace it via override.
  • Runtime Polymorphism: A base reference pointing to a derived instance (Car carRef = new SportsCar()) executes the overridden method at runtime, contrasting with Method Hiding in C#.

Static Binding in C#

Static binding, also called early binding, occurs when the compiler determines which method to execute at compile time. It is used in compile-time polymorphism, such as method overloading and operator overloading.

Key Points of Static Binding in C#

  • Resolved at Compile Time: The C# compiler matches the method signature before the program runs.
  • Used in Overloading: Occurs in method overloading, operator overloading, and private, static, or sealed method calls.
  • Faster Execution: Requires less runtime overhead than dynamic binding because the target method is already known.
  • Opposite of Dynamic Binding: Contrasts with late binding used in method overriding (see Dynamic Binding in C#).

C# Static Binding Code Example (Method Overloading)

using System;

class Calculator
{
    public int Add(int a, int b)
    {
        return a + b;
    }

    public double Add(double a, double b)
    {
        return a + b;
    }
}

class Program
{
    static void Main()
    {
        Calculator calc = new Calculator();
        Console.WriteLine(calc.Add(2, 3));     // Calls int Add(int, int)
        Console.WriteLine(calc.Add(2.5, 3.5)); // Calls double Add(double, double)
    }
}

Console Output

5
6

How This Example Works

  • Signature Matching: Passing integers (2, 3) binds directly to Add(int, int) during compilation.
  • Overload Resolution: Passing floating-point values (2.5, 3.5) binds to Add(double, double) without waiting for runtime.

Dynamic Binding in C#

Dynamic binding (late binding) determines the executed method at runtime to enable runtime polymorphism in C#.

Key Points of Dynamic Binding in C#

  • Resolved at Runtime: The CLR selects the method during execution.
  • Method Overriding & Keywords: Requires base methods marked virtual/abstract and derived methods marked override.
  • Base Reference Polymorphism: A Car reference pointing to a SportsCar object calls the derived implementation (unlike Static Binding in C#).

C# Dynamic Binding Code Example (Method Overriding)

using System;

class Car
{
    public virtual void Drive() => Console.WriteLine("Car is driving.");
}

class SportsCar : Car
{
    public override void Drive() => Console.WriteLine("SportsCar is driving fast!");
}

class Program
{
    static void Main()
    {
        Car c = new SportsCar();
        c.Drive();
    }
}

Console Output

SportsCar is driving fast!

How This Example Works

  • Reference vs. Object Type: Variable c is compiled as type Car but instantiated as SportsCar at runtime.
  • Late Binding Dispatch: Virtual dispatch invokes SportsCar.Drive() dynamically.

Function Overriding in C#

Function overriding (also called method overriding) means defining the same method in a derived class that already exists in the base class, using the exact same method name, parameters, and return type. It allows a child class to provide its own specialized behavior for an inherited method.

Key Rules of Function Overriding in C#

  • Requires virtual in Base Class: The base class method must be marked with virtual, abstract, or override to permit overriding.
  • Requires override in Derived Class: The child class must explicitly use the override keyword.
  • Exact Signature Match: The method name, parameter list, and return type must match the base method exactly.
  • Cannot Override Private or Static Methods: Methods marked private, static, or sealed cannot be overridden.
  • Enables Runtime Polymorphism: The method executed is chosen at runtime based on the actual object type.

C# Function Overriding Code Example

using System;

class Shape
{
    public virtual void Draw()
    {
        Console.WriteLine("Drawing a generic shape.");
    }
}

class Circle : Shape
{
    public override void Draw()
    {
        Console.WriteLine("Drawing a circle.");
    }
}

class Program
{
    static void Main()
    {
        Shape s = new Circle();
        s.Draw(); // Calls Circle's overridden Draw() method
    }
}

Console Output

Drawing a circle.

Calling the Base Method with base

If you still want to run the original parent logic inside your overridden method, call base.Draw(); inside the derived class. Learn more in The 'base' Keyword in C#.