0Pricing
C# Academy · Lesson

Polymorphism and Method Overriding

Master polymorphism to create flexible and reusable code through method overriding.

Polymorphism and Method Overriding is a free C# Academy lesson on CoddyKit — lesson 7 of 7. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C# Academy learning path, one of 7 lessons in the course, and your progress syncs across the web and the CoddyKit app.

1

Polymorphism and Method Overriding

Welcome to the next lesson! In this lesson, you’ll learn about polymorphism, an essential concept of Object-Oriented Programming, and how it allows you to override methods to provide flexible and reusable code. Let’s get started!

Polymorphism and Method Overriding — illustration 1

2

What is Polymorphism?

Polymorphism means "many forms" and allows objects of different classes to be treated as objects of a common base class. This enables flexibility and reusability in your code.

Example: A method can behave differently depending on the type of object it is working with.

Key Point: Polymorphism is achieved in C# through method overriding and interfaces.

3

Understanding Method Overriding

Method overriding allows a derived class to provide a specific implementation of a method that is already defined in its base class. Use the virtual keyword in the base class and the override keyword in the derived class. Example:

Tip: Method overriding ensures that the correct method is called for an object, regardless of its reference type.

using System;

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

class Dog : Animal {
    public override void Speak() {
        Console.WriteLine("The dog barks.");
    }
}

class Cat : Animal {
    public override void Speak() {
        Console.WriteLine("The cat meows.");
    }
}

class Program {
    static void Main() {
        Animal myDog = new Dog();
        Animal myCat = new Cat();

        myDog.Speak(); // Outputs: The dog barks.
        myCat.Speak(); // Outputs: The cat meows.
    }
}

4

Using Polymorphism

Polymorphism allows you to write code that works with different types of objects through a common interface. Example:

Key Point: Polymorphism allows you to write more generic and reusable code.

using System;

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

class Dog : Animal {
    public override void Speak() {
        Console.WriteLine("The dog barks.");
    }
}

class Cat : Animal {
    public override void Speak() {
        Console.WriteLine("The cat meows.");
    }
}

class Program {
    static void Main() {
        Animal[] animals = { new Dog(), new Cat() };

        foreach (Animal animal in animals) {
            animal.Speak(); // Calls the appropriate method for each object
        }
    }
}

5

Using the Base Keyword with Overriding

The base keyword is used to call a method from the base class in the overridden method. Example:

Tip: Use base when you want to retain base class functionality in an overridden method.

using System;

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

class Dog : Animal {
    public override void Speak() {
        base.Speak(); // Calls the base class method
        Console.WriteLine("The dog barks.");
    }
}

class Program {
    static void Main() {
        Dog myDog = new Dog();
        myDog.Speak(); // Outputs: The animal makes a sound. The dog barks.
    }
}

6

Abstract Classes and Methods

Abstract classes provide a blueprint for derived classes. They can include abstract methods, which must be overridden in the derived class. Example:

Key Point: Abstract methods enforce that derived classes provide their own implementation.

using System;

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

class Dog : Animal {
    public override void Speak() {
        Console.WriteLine("The dog barks.");
    }
}

class Program {
    static void Main() {
        Animal myDog = new Dog();
        myDog.Speak(); // Outputs: The dog barks.
    }
}

7

Practice Challenge

Create an abstract class Shape with an abstract method CalculateArea(). Derive classes Rectangle and Circle that implement CalculateArea(). Example:

using System;

abstract class Shape {
    public abstract double CalculateArea();
}

class Rectangle : Shape {
    public double Width { get; set; }
    public double Height { get; set; }

    public override double CalculateArea() {
        return Width * Height;
    }
}

class Circle : Shape {
    public double Radius { get; set; }

    public override double CalculateArea() {
        return Math.PI * Radius * Radius;
    }
}

class Program {
    static void Main() {
        Shape rectangle = new Rectangle { Width = 4, Height = 5 };
        Shape circle = new Circle { Radius = 3 };

        Console.WriteLine("Rectangle Area: " + rectangle.CalculateArea());
        Console.WriteLine("Circle Area: " + circle.CalculateArea());
    }
}

8

10

Best Practices for Polymorphism

Here are some best practices:

  • Use polymorphism to simplify code and avoid duplication.
  • Override only those methods that require specific behavior in derived classes.
  • Use abstract classes and methods for shared interfaces across derived classes.

Tip: Polymorphism makes your code easier to extend and maintain.

12

Great Job!

Congratulations! You’ve learned how to use polymorphism and method overriding to write flexible and reusable code. In the next lesson, we’ll explore working with interfaces to achieve abstraction and implement multiple behaviors. Let’s keep coding!

Polymorphism and Method Overriding — illustration 10

Frequently asked questions

Is the “Polymorphism and Method Overriding” lesson free?

Yes — the full text of “Polymorphism and Method Overriding” is free to read here on the web, and the C# Academy course includes 7 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C# Academy course, upgrade to CoddyKit PRO.

What will I learn in “Polymorphism and Method Overriding”?

Master polymorphism to create flexible and reusable code through method overriding. You practise C# Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start C# Academy?

No prior experience is required. C# Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 7 of 7, so you can start here or from the beginning and move at your own pace.

How long does the “Polymorphism and Method Overriding” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this C# Academy lesson?

Yes. Every C# Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. What is Object-Oriented Programming?
  2. Defining Classes and Objects
  3. Understanding Properties and Methods
  4. Constructors in C#
  5. Encapsulation and Access Modifiers
  6. Inheritance: Reusing Code
  7. Polymorphism and Method Overriding
← Back to C# Academy