C# / Object-Oriented Programming in C#
Understanding Inheritance and Polymorphism
This tutorial will cover the concepts of inheritance and polymorphism in C#. You will learn how to create subclasses, override methods, and leverage the power of polymorphism.
Section overview
5 resourcesCovers object-oriented programming concepts such as classes, objects, inheritance, and polymorphism.
Understanding Inheritance and Polymorphism in C
1. Introduction
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:
- Understand the concept of inheritance and how to create subclasses in C#
- Understand the concept of polymorphism and how to override methods
- Utilize inheritance and polymorphism to create more efficient and organized code
Prerequisites: A basic understanding of C# syntax and object-oriented programming concepts such as classes and objects.
2. Step-by-Step Guide
Inheritance
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
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.
3. Code Examples
Code Example 1
// 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.
4. Summary
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.
5. Practice Exercises
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.
Need Help Implementing This?
We build custom systems, plugins, and scalable infrastructure.
Related topics
Keep learning with adjacent tracks.
Popular tools
Helpful utilities for quick tasks.
Latest articles
Fresh insights from the CodiWiki team.
AI in Drug Discovery: Accelerating Medical Breakthroughs
In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…
Read articleAI in Retail: Personalized Shopping and Inventory Management
In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …
Read articleAI in Public Safety: Predictive Policing and Crime Prevention
In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…
Read articleAI in Mental Health: Assisting with Therapy and Diagnostics
In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…
Read articleAI in Legal Compliance: Ensuring Regulatory Adherence
In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…
Read article