Monday, 17 November 2025

Compile-time Polymorphism

Compile-time Polymorphism

Compile-time Polymorphism in C#

Compile-time polymorphism, also called static polymorphism, means that the method to be executed is decided during compile time. It is mainly achieved through method overloading in C#.

Features:

  • Decided at compile time
  • Achieved using method overloading
  • Occurs within the same class
  • Faster than runtime polymorphism

C# Example:

class Calculator { public int Add(int a, int b) { return a + b; } public double Add(double a, double b) { return a + b; } public int Add(int a, int b, int c) { return a + b + c; } } class Program { static void Main() { Calculator c = new Calculator(); Console.WriteLine(c.Add(2, 3)); Console.WriteLine(c.Add(2.5, 3.1)); Console.WriteLine(c.Add(1, 2, 3)); } }

Runtime Polymorphism

Runtime Polymorphism

Runtime Polymorphism in C#

Runtime polymorphism (dynamic polymorphism) means that the method to be executed is decided during runtime. It is achieved through method overriding using the virtual and override keywords.

Features:

  • Achieved using method overriding
  • Uses virtual and override
  • Method selection happens at runtime
  • Enables dynamic and flexible behavior

C# Example:

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(); } }

Output:

SportsCar is driving fast!

Overloading vs Overriding

Overloading vs Overriding

Difference Between Overloading and Overriding in C#

1. Method Overloading

  • Same method name, different parameters.
  • Occurs within the same class.
  • Compile-time (static) polymorphism.
  • Parameters must change (type, number, or order).

C# Example:

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

2. Method Overriding

  • Same method name, same parameters.
  • Occurs in different classes using inheritance.
  • Runtime (dynamic) polymorphism.
  • Uses virtual and override keywords.

C# Example:

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

C# Inheritance Concepts

C# Inheritance Concepts

C# Inheritance & Keywords

1. The sealed Keyword (C# version of Java's final)

C# does not use the keyword final. Instead, it uses sealed to:

  • Stop a class from being inherited
  • Stop a method from being overridden again

Sealed Class Example

sealed class Car { }

Sealed Method Example

class A { public virtual void Show() { } } class B : A { public sealed override void Show() { } }

2. Multilevel Inheritance Example Using Car Classes

Multilevel inheritance means:

Car → SportsCar → RaceCar

C# Example

class Car // Level 1 (Base Class) { public void Start() { Console.WriteLine("Car starts."); } } class SportsCar : Car // Level 2 (Inherits from Car) { public void Turbo() { Console.WriteLine("SportsCar activates turbo."); } } class RaceCar : SportsCar // Level 3 (Inherits from SportsCar) { public void Nitro() { Console.WriteLine("RaceCar boosts with nitro!"); } } class Program { static void Main() { RaceCar rc = new RaceCar(); rc.Start(); // from Car rc.Turbo(); // from SportsCar rc.Nitro(); // from RaceCar } }

Output:

Car starts. SportsCar activates turbo. RaceCar boosts with nitro!

Saturday, 15 November 2025

Multiple Inheritance in C#

Multiple Inheritance in C#

Can a Class Inherit from Multiple Classes in C#?

No. In C#, a class cannot inherit from more than one class directly. This restriction is in place to avoid ambiguity problems such as the diamond problem that can occur in multiple inheritance.

Example (Not Allowed)

class A { }
class B { }

class C : A, B { }  // ❌ This is NOT allowed in C#
    

How C# Handles Multiple Inheritance

  • C# supports multiple inheritance via interfaces. A class can implement multiple interfaces.
  • This avoids ambiguity because interfaces only define method signatures, and the implementing class provides the actual implementation.

Example Using Interfaces

interface IA {
    void MethodA();
}

interface IB {
    void MethodB();
}

class C : IA, IB {
    public void MethodA() {
        Console.WriteLine("MethodA implementation");
    }

    public void MethodB() {
        Console.WriteLine("MethodB implementation");
    }
}
    

Summary: C# does not allow multiple class inheritance, but multiple interfaces can be implemented to achieve similar behavior.

The 'base' Keyword in C#

The 'base' Keyword in C#

The base Keyword in C#

In C#, the base keyword is used to refer to the immediate parent class of the current class. It is similar to the super keyword in Java.

Main Uses of base

  1. Access parent class methods: Call a method in the base class that is overridden in the derived class.
  2. Access parent class constructors: Invoke a constructor of the base class from the derived class constructor.
  3. Access parent class properties or fields: Access members of the base class that are hidden in the derived class.

Examples

1. Calling Base Class Method

class Parent {
    public void Show() {
        Console.WriteLine("Parent Show");
    }
}

class Child : Parent {
    public new void Show() {
        Console.WriteLine("Child Show");
    }

    public void Display() {
        base.Show(); // calls Parent's Show()
    }
}
    

2. Calling Base Class Constructor

class Parent {
    public Parent() {
        Console.WriteLine("Parent Constructor");
    }
}

class Child : Parent {
    public Child() : base() {  // calls Parent constructor
        Console.WriteLine("Child Constructor");
    }
}
    

Note: Use base when you want to explicitly access members or constructors of the parent class from a derived class.

Diamond Problem in Inheritance

Diamond Problem in Inheritance

The Diamond Problem

The diamond problem occurs in multiple inheritance when a class inherits from two classes that both inherit from a common base class. This creates ambiguity about which base class method or property should be used.

Illustration of the Diamond Problem

        A
       / \
      B   C
       \ /
        D
    

Here:

  • Class A is the base class.
  • Classes B and C inherit from A.
  • Class D inherits from both B and C.

Problem: If A has a method Show() and both B and C override it, which version should D inherit? This ambiguity is called the diamond problem.

How C# Handles It

  • C# does not allow multiple class inheritance, so the diamond problem is avoided for classes.
  • Using interfaces, C# allows multiple inheritance without ambiguity because interface methods must be explicitly implemented.

Example with Interfaces in C#

interface IA {
    void Show();
}

interface IB : IA { }
interface IC : IA { }

class D : IB, IC {
    public void Show() {
        Console.WriteLine("Implemented Show in D");
    }
}
    

Here, D explicitly implements the method, resolving any ambiguity.