Inheritance and Polymorphism in C#
Inheritance and Polymorphism are fundamental concepts in object-oriented programming (OOP) that allow for code reuse and flexibility. Understanding these concepts is crucial for building scalable and maintainable applications in C#.
1. Inheritance
Inheritance is a mechanism that allows a class to inherit properties and methods from another class. The class that inherits is called the derived class or subclass, and the class being inherited from is called the base class or superclass. Inheritance promotes code reuse and the creation of a hierarchical relationship between classes.
Example
class Animal { public string Name { get; set; } public void Eat() { Console.WriteLine(Name + " is eating."); } } class Dog : Animal { public void Bark() { Console.WriteLine(Name + " is barking."); } } class Program { static void Main(string[] args) { Dog myDog = new Dog(); myDog.Name = "Buddy"; myDog.Eat(); // Inherited from Animal myDog.Bark(); // Defined in Dog } }
In this example, the Dog
class inherits from the Animal
class. The Dog
class can use the Eat
method defined in the Animal
class, and it also has its own Bark
method.
2. Polymorphism
Polymorphism allows objects of different classes to be treated as objects of a common superclass. It enables methods to be written that can work with objects of multiple types. Polymorphism is often achieved through method overriding and interfaces.
Method Overriding
Method overriding allows a subclass to provide a specific implementation of a method that is already defined in its superclass. This is done using the override
keyword.
Example
class Animal { public virtual void MakeSound() { Console.WriteLine("The animal makes a sound."); } } class Dog : Animal { public override void MakeSound() { Console.WriteLine("The dog barks."); } } class Cat : Animal { public override void MakeSound() { Console.WriteLine("The cat meows."); } } class Program { static void Main(string[] args) { Animal myAnimal = new Animal(); Animal myDog = new Dog(); Animal myCat = new Cat(); myAnimal.MakeSound(); // Output: The animal makes a sound. myDog.MakeSound(); // Output: The dog barks. myCat.MakeSound(); // Output: The cat meows. } }
In this example, the MakeSound
method is overridden in both the Dog
and Cat
classes. When the method is called on objects of these classes, the specific implementation for each class is executed.
Interfaces
Interfaces define a contract that classes must adhere to. A class that implements an interface must provide implementations for all its methods. This allows for polymorphism by enabling objects of different classes to be treated as objects of the interface type.
Example
interface IShape { void Draw(); } class Circle : IShape { public void Draw() { Console.WriteLine("Drawing a circle."); } } class Square : IShape { public void Draw() { Console.WriteLine("Drawing a square."); } } class Program { static void Main(string[] args) { IShape myCircle = new Circle(); IShape mySquare = new Square(); myCircle.Draw(); // Output: Drawing a circle. mySquare.Draw(); // Output: Drawing a square. } }
In this example, both the Circle
and Square
classes implement the IShape
interface. This allows objects of these classes to be treated as IShape
objects, enabling polymorphic behavior.
Conclusion
Inheritance and Polymorphism are powerful tools in C# that enhance code reuse and flexibility. By understanding and applying these concepts, you can create more modular and maintainable applications. Inheritance allows for the creation of hierarchical relationships between classes, while Polymorphism enables methods to work with objects of different types, making your code more adaptable and extensible.