Codecademy Logo

Learn C#: Polymorphism

Virtual Methods in C#

In C#, a virtual method is defined in a base class and can be overridden in a derived class. This allows for dynamic method binding, where the derived class’s method is called even when using a base class reference.

class Employee {
public virtual void MakeHRRequest() {
Console.WriteLine("Employee makes an HR request.");
}
}
class Manager : Employee {
public override void MakeHRRequest() {
Console.WriteLine("Manager makes an HR request.");
}
}

is Operator in C#

The is operator in C# checks if a reference can be cast to a specified type. It returns True if the conversion is possible and False otherwise.

public class Animal
{
public void makeSound()
{
Console.WriteLine("Animal makes a sound");
}
}
public class Dog : Animal
{
public void makeSound()
{
Console.WriteLine("Dog barks");
}
}
public class Program
{
public static void Main(string[] args)
{
Dog myDog = new Dog();
Console.WriteLine(myDog is Animal); // Outputs True
}
}

as Operator in C#

The as operator in C# attempts to downcast a reference to a specified type. If the downcast fails, it returns null instead of throwing an exception.

public class Animal
{
public virtual void makeSound()
{
Console.WriteLine("Animal makes a sound");
}
}
public class Dog : Animal
{
public override void makeSound()
{
Console.WriteLine("Dog barks");
}
public void move()
{
Console.WriteLine("Dog walks");
}
}
public class Program
{
public static void Main(string[] args)
{
Animal myAnimal = new Dog();
// myAnimal.move(); // Will not work
Dog myDog = myAnimal as Dog;
if (myDog != null)
{
myDog.makeSound(); // Output: Dog barks
myDog.move(); // Output: Dog walks
}
}
}

Downcasting in C#

Downcasting in C# is the explicit conversion from a base class reference to a derived class reference. It requires a cast operator and can throw an exception if the object isn’t of the derived type.

Employee myEmployee = new Manager(); // Upcasting to Employee
Manager myManager = (Manager)myEmployee; // Downcasting to Manager

Upcasting in C#

Upcasting in C# is the implicit conversion from a derived class to a base class. This allows a derived class object to be treated as an instance of its base class.

Manager myManager = new Manager();
Employee myEmployee = myManager; // Upcasting to Employee

Abstract Classes in C#

An abstract class in C# cannot be instantiated and is meant to be a base class for other classes. It can contain both abstract methods (without implementation) and concrete methods (with implementation).

abstract class Employee {
public abstract void MakeHRRequest(); // Must be implemented by the derived class
public void ClockIn() {
Console.WriteLine("Employee clocks in.");
}
}

Abstract Methods in C#

An abstract method in C# is declared within an abstract class and does not have an implementation. It must be overridden in any derived class that inherits from the abstract class.

abstract class Employee {
public abstract void MakeHRRequest();
}
class Manager : Employee {
public override void MakeHRRequest() {
Console.WriteLine("Manager makes an HR request.");
}
}

Polymorphism in C#

In C#, references are polymorphic, meaning a variable of a base type can refer to an object of a derived class. This allows for flexibility in code, enabling methods to operate on base class references, regardless of the specific subclass type.

Learn more on Codecademy