Abstract Classes and Interfaces in C#: Key Differences with Examples

Abstract Classes and Interfaces in C#: Key Differences with Examples

Abstract Classes and Interfaces in C#: Key Differences with Examples

Understanding the difference between abstract classes and interfaces is essential for mastering object-oriented programming in C#. Both play a crucial role in designing flexible and extensible applications. Let’s dive into their definitions, differences, and real-world examples.

What is an Abstract Class?

An abstract class is a class that cannot be instantiated on its own. It can have both abstract methods (without implementation) and concrete methods (with implementation). Abstract classes are typically used as base classes in a class hierarchy.

Example of Abstract Class

abstract class Animal { public abstract void MakeSound(); // Abstract method public void Sleep() { Console.WriteLine("Sleeping..."); } } class Dog : Animal { public override void MakeSound() { Console.WriteLine("Bark"); } } class Program { static void Main() { Animal myDog = new Dog(); myDog.MakeSound(); // Output: Bark myDog.Sleep(); // Output: Sleeping... } }

What is an Interface?

An interface defines a contract that classes must implement. It only contains method signatures and does not provide any implementation. A class can implement multiple interfaces, which makes them suitable for achieving multiple inheritance in C#.

Example of Interface

interface IAnimal { void MakeSound(); } interface IPet { void Play(); } class Cat : IAnimal, IPet { public void MakeSound() { Console.WriteLine("Meow"); } public void Play() { Console.WriteLine("Playing with a ball"); } } class Program { static void Main() { Cat myCat = new Cat(); myCat.MakeSound(); // Output: Meow myCat.Play(); // Output: Playing with a ball } }

Key Differences

  • Instantiation: Abstract classes cannot be instantiated, while interfaces must be implemented by classes.
  • Implementation: Abstract classes can have both implemented and abstract methods, whereas interfaces only have method declarations.
  • Multiple Inheritance: A class can implement multiple interfaces, but it can inherit from only one abstract class.
  • Constructors: Abstract classes can have constructors; interfaces cannot.

When to Use Abstract Classes vs Interfaces

  • Use abstract classes when you want to share code among closely related classes.
  • Use interfaces to define a contract that can be implemented by any class, regardless of its position in the hierarchy.

Real-World Example

Abstract Class Example

abstract class Vehicle { public abstract void StartEngine(); public void StopEngine() { Console.WriteLine("Engine stopped."); } } class Car : Vehicle { public override void StartEngine() { Console.WriteLine("Car engine started."); } }

Interface Example

interface IVehicle { void StartEngine(); void StopEngine(); } class Bike : IVehicle { public void StartEngine() { Console.WriteLine("Bike engine started."); } public void StopEngine() { Console.WriteLine("Bike engine stopped."); } }
Abstract classes and interfaces serve distinct purposes in C#. Use them wisely to create flexible and maintainable code.
 This Content Sponsored by Buymote Shopping app

BuyMote E-Shopping Application is One of the Online Shopping App

Now Available on Play Store & App Store (Buymote E-Shopping)

Click Below Link and Install Application: https://buymote.shop/links/0f5993744a9213079a6b53e8

Sponsor Content: #buymote #buymoteeshopping #buymoteonline #buymoteshopping #buymoteapplication
  

Post a Comment

0 Comments