C# Inheritance is one of the most important pillars of Object-Oriented Programming (OOP). Inheritance in C# allows a class to acquire the properties, methods, and behaviors of another class. This promotes code reusability, scalability, and maintainability in modern software development.
In simple terms, inheritance enables you to create a new class (derived class) from an existing class (base class). The derived class automatically gets access to the public and protected members of the base class. This powerful feature helps developers build structured, modular, and reusable C# applications.
Inheritance in C# is a mechanism where one class inherits the members of another class. The class that provides the inherited members is called the Base Class (or Parent Class), and the class that inherits those members is called the Derived Class (or Child Class).
Inheritance supports the βis-aβ relationship. For example, if Dog inherits from Animal, then Dog is an Animal.
using System;
class Animal
{
public void Eat()
{
Console.WriteLine("Animal is eating");
}
}
class Dog : Animal
{
public void Bark()
{
Console.WriteLine("Dog is barking");
}
}
class Program
{
static void Main()
{
Dog myDog = new Dog();
myDog.Eat();
myDog.Bark();
}
}
In the above example:
Inheritance provides several advantages in C# application development:
Instead of rewriting common functionality in multiple classes, you define it once in a base class and reuse it in derived classes.
Changes made in the base class automatically reflect in derived classes, reducing redundancy and errors.
New features can be added by extending existing classes without modifying original code.
Inheritance enables runtime polymorphism using virtual and override keywords.
C# supports different types of inheritance:
A derived class inherits from one base class.
class Parent
{
public void Display()
{
Console.WriteLine("Parent Class");
}
}
class Child : Parent
{
}
A class inherits from a derived class.
class GrandParent
{
public void Show()
{
Console.WriteLine("GrandParent Class");
}
}
class Parent : GrandParent
{
}
class Child : Parent
{
}
Multiple classes inherit from one base class.
class Animal
{
public void Eat()
{
Console.WriteLine("Eating");
}
}
class Dog : Animal
{
}
class Cat : Animal
{
}
C# does not support multiple inheritance with classes, but it supports multiple inheritance through interfaces.
interface IWalk
{
void Walk();
}
interface IRun
{
void Run();
}
class Person : IWalk, IRun
{
public void Walk()
{
Console.WriteLine("Walking");
}
public void Run()
{
Console.WriteLine("Running");
}
}
Access modifiers control visibility of base class members in derived classes.
Accessible everywhere.
Accessible within the class and derived classes.
Not accessible in derived classes.
Accessible within the same assembly.
Accessible within assembly or derived classes.
The base keyword is used to access members of the base class from a derived class.
class Animal
{
public string name = "Animal";
public void Display()
{
Console.WriteLine("Base class method");
}
}
class Dog : Animal
{
public void Show()
{
Console.WriteLine(base.name);
base.Display();
}
}
Method overriding allows a derived class to provide a specific implementation of a method already defined in the base class.
class Animal
{
public virtual void Sound()
{
Console.WriteLine("Animal sound");
}
}
class Dog : Animal
{
public override void Sound()
{
Console.WriteLine("Dog barks");
}
}
The virtual keyword allows a method to be overridden, and the override keyword provides a new implementation in the derived class.
The sealed keyword prevents further inheritance of a class or overriding of a method.
sealed class FinalClass
{
}
An abstract class cannot be instantiated and may contain abstract methods that must be implemented by derived classes.
abstract class Shape
{
public abstract void Draw();
}
class Circle : Shape
{
public override void Draw()
{
Console.WriteLine("Drawing Circle");
}
}
Constructors are not inherited, but derived class constructors can call base class constructors using the base keyword.
class Person
{
public Person(string name)
{
Console.WriteLine("Name: " + name);
}
}
class Student : Person
{
public Student(string name) : base(name)
{
Console.WriteLine("Student Constructor");
}
}
Inheritance enables runtime polymorphism in C#. When a base class reference points to a derived class object, overridden methods are executed.
Animal obj = new Dog();
obj.Sound();
This concept is essential in large-scale C# application development.
class Vehicle
{
public virtual void Start()
{
Console.WriteLine("Vehicle Starting");
}
}
class Car : Vehicle
{
public override void Start()
{
Console.WriteLine("Car Starting with Key");
}
}
class Bike : Vehicle
{
public override void Start()
{
Console.WriteLine("Bike Starting with Kick");
}
}
This demonstrates runtime polymorphism and method overriding in C#.
C# Inheritance is a powerful feature of Object-Oriented Programming in C#. It enables code reusability, modular design, and runtime polymorphism. By understanding base classes, derived classes, method overriding, abstract classes, and access modifiers, developers can build scalable and maintainable applications.
Mastering inheritance in C# is essential for developing enterprise-level applications, ASP.NET projects, desktop applications, and modern software systems.
C# is primarily used on the Windows .NET framework, although it can be applied to an open source platform. This highly versatile programming language is an object-oriented programming language (OOP) and comparably new to the game, yet a reliable crowd pleaser.
The C# language is also easy to learn because by learning a small subset of the language you can immediately start to write useful code. More advanced features can be learnt as you become more proficient, but you are not forced to learn them to get up and running. C# is very good at encapsulating complexity.
The decision to opt for C# or Node. js largely hinges on the specific requirements of your project. If you're developing a CPU-intensive, enterprise-level application where stability and comprehensive tooling are crucial, C# might be your best bet.
C# is part of .NET, a free and open source development platform for building apps that run on Windows, macOS, Linux, iOS, and Android. There's an active community answering questions, producing samples, writing tutorials, authoring books, and more.
Copyrights © 2024 letsupdateskills All rights reserved