This tutorial aims to provide an in-depth understanding of two fundamental object-oriented programming concepts: inheritance and polymorphism, using C# as the language of choice.
By the end of this tutorial, you will be able to:
Prerequisites: A basic understanding of C# syntax and object-oriented programming concepts such as classes and objects.
Inheritance allows us to define a class in terms of another class, which makes creating and maintaining an application easier. This also provides an opportunity to reuse the code functionality and fast implementation time.
// Base class
public class Animal {
public virtual void animalSound() {
Console.WriteLine("The animal makes a sound");
}
}
Here, Animal
is a base class, and the function animalSound()
is marked as virtual
, which means it can be overridden in the subclass.
Polymorphism means "many forms", and it occurs when we have many classes that are related to each other by inheritance.
// Derived classes
public class Pig : Animal {
public override void animalSound() {
Console.WriteLine("The pig says: wee wee");
}
}
public class Dog : Animal {
public override void animalSound() {
Console.WriteLine("The dog says: bow wow");
}
}
Here, Pig
and Dog
are derived classes that inherit from the Animal
base class. Both classes override the animalSound()
method.
// Driver code
public class Program {
public static void Main(string[] args) {
Animal myAnimal = new Animal(); // Create a Animal object
Animal myPig = new Pig(); // Create a Pig object
Animal myDog = new Dog(); // Create a Dog object
myAnimal.animalSound(); // Animal: The animal makes a sound
myPig.animalSound(); // Pig: The pig says: wee wee
myDog.animalSound(); // Dog: The dog says: bow wow
}
}
This is the driver code, where we create objects of the Animal
, Pig
, and Dog
classes, and call their animalSound()
methods. The output will be as commented in the code.
In this tutorial, we've covered the basics of inheritance and polymorphism. We've learned how to create subclasses, how to override methods, and how to make efficient use of these concepts to make our code more organized and reusable.
Next, you can explore other object-oriented concepts like abstraction and encapsulation.
Exercise 1: Create a base class Car
with a method carSound()
that outputs "The car makes a sound". Create two subclasses BMW
and Mercedes
that override the carSound()
method to output "BMW says: brum brum" and "Mercedes says: vroom vroom", respectively.
Exercise 2: Modify the Car
class to include a model
property. Add a constructor method to each subclass that accepts a model
parameter and sets the model
property. Override the carSound()
method in each subclass to include the model in the output, for example, "The BMW X5 says: brum brum".
Exercise 3: Create a list of Car
objects that includes instances of both the BMW
and Mercedes
subclasses. Loop over the list and call the carSound()
method for each object.
Note: Practice is essential in learning programming. Try to modify the code or experiment with it to see different outcomes.