๐ŸŽ“ A1Learner
๐Ÿ”

C# Polymorphism

Polymorphism lets a base type hold objects of different derived classes. A call on that variable runs the method of the actual object.

A base variable, derived answer

The variable has the type Animal and holds a Dog. The Sound call runs the Dog version.

using System;

class Animal
{
    public virtual void Sound()
    {
        Console.WriteLine("The animal makes a sound");
    }
}

class Dog : Animal
{
    public override void Sound()
    {
        Console.WriteLine("Woof");
    }
}

class Program
{
    static void Main(string[] args)
    {
        Animal myAnimal = new Dog();
        myAnimal.Sound();
    }
}

Two objects, two answers

Both variables have the type Animal. One holds a Dog and the other holds a Cat.

using System;

class Animal
{
    public virtual void Sound()
    {
        Console.WriteLine("The animal makes a sound");
    }
}

class Dog : Animal
{
    public override void Sound()
    {
        Console.WriteLine("Woof");
    }
}

class Cat : Animal
{
    public override void Sound()
    {
        Console.WriteLine("Meow");
    }
}

class Program
{
    static void Main(string[] args)
    {
        Animal first = new Dog();
        Animal second = new Cat();
        first.Sound();
        second.Sound();
    }
}

A loop over animals

The array holds three objects of two classes. The loop calls Sound on each object in turn.

using System;

class Animal
{
    public virtual void Sound()
    {
        Console.WriteLine("The animal makes a sound");
    }
}

class Dog : Animal
{
    public override void Sound()
    {
        Console.WriteLine("Woof");
    }
}

class Cat : Animal
{
    public override void Sound()
    {
        Console.WriteLine("Meow");
    }
}

class Program
{
    static void Main(string[] args)
    {
        Animal[] animals = new Animal[] { new Dog(), new Cat(), new Dog() };
        foreach (Animal animal in animals)
        {
            animal.Sound();
        }
    }
}

Example

using System;

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

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

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

class Program
{
    static void Main(string[] args)
    {
        Shape first = new Circle();
        Shape second = new Square();
        first.Draw();
        second.Draw();
    }
}

Key points

  • A variable of a base type can hold a derived object.
  • The virtual keyword marks a base method that can be replaced.
  • The override keyword replaces a base method in a derived class.
  • The program picks the method version when it runs.
  • The same call can print different lines for different objects.
  • An override method needs the same name as the base method.

In this example, both variables have the type Shape. The first holds a Circle, so its Draw call prints the circle line. The second holds a Square, so its Draw call prints the square line.

Try it Yourself

Edit the code, then press Run.

Running is simulated ยท Programiz C# Compiler

Exercise

Animal pet = new Dog(); and Dog overrides Sound. Which version runs on pet.Sound()?