Understanding abstract and sealed classes in C# is crucial for mastering object-oriented programming (OOP) concepts. These class types provide developers with powerful tools for controlling inheritance and designing scalable, maintainable code. In this comprehensive guide, we will explore the differences between abstract and sealed classes, their real-world applications, and practical C# examples.
An abstract class in C# is a class that cannot be instantiated on its own and is meant to serve as a base class for other classes. Abstract classes can contain:
using System; abstract class Vehicle { public abstract void Start(); public void Honk() { Console.WriteLine("Honking..."); } } class Car : Vehicle { public override void Start() { Console.WriteLine("Car is starting..."); } } class Program { static void Main() { Car myCar = new Car(); myCar.Start(); // Output: Car is starting... myCar.Honk(); // Output: Honking... } }
In this example, the Vehicle class is abstract and cannot be instantiated directly. The Car class inherits from Vehicle and provides an implementation for the abstract Start method.
A sealed class in C# is a class that cannot be inherited. Sealed classes are useful when you want to restrict further derivation, ensuring that the implementation remains final and unmodified.
In C#, a non-abstract method is a method that has a complete implementation in the class where it is defined. Unlike abstract methods, which only define the method signature and require derived classes to provide an implementation, non-abstract methods can be called directly on an object of the class (if the class itself is not abstract or is instantiated through a derived class).
virtual.using System; abstract class Animal { // Abstract method (must be implemented in derived class) public abstract void MakeSound(); // Non-abstract method (has implementation) public void Sleep() { Console.WriteLine("The animal is sleeping..."); } } class Dog : Animal { // Implementation of abstract method public override void MakeSound() { Console.WriteLine("Dog says: Woof Woof!"); } } class Program { static void Main() { Dog myDog = new Dog(); myDog.MakeSound(); // Output: Dog says: Woof Woof! myDog.Sleep(); // Output: The animal is sleeping... } }
In this example:
using System; class Vehicle { public void Start() { Console.WriteLine("Vehicle is starting..."); } } class Car : Vehicle { // Inherits Start() from Vehicle } class Program { static void Main() { Car myCar = new Car(); myCar.Start(); // Output: Vehicle is starting... } }
Here, Start() is a non-abstract method in the base class Vehicle. The derived class Car inherits it directly without needing to implement it, demonstrating code reuse.
using System; sealed class Logger { public void Log(string message) { Console.WriteLine($"Log entry: {message}"); } } // The following line would produce an error // class AdvancedLogger : Logger {} class Program { static void Main() { Logger logger = new Logger(); logger.Log("Application started."); // Output: Log entry: Application started. } }
The Logger class is sealed and cannot be extended. This is particularly useful for classes that provide critical functionality you don’t want altered.
| Feature | Abstract Class | Sealed Class |
|---|---|---|
| Instantiation | Cannot instantiate directly | Can instantiate |
| Inheritance | Can be inherited | Cannot be inherited |
| Methods | Can have abstract and concrete methods | Can have only concrete methods |
| Use Case | Defines a common base for derived classes | Prevents further inheritance for security/performance |
Understanding abstract and sealed classes in C# is essential for any C# developer. Abstract classes enable polymorphism, code reuse, and a clear design structure, while sealed classes ensure security, performance, and controlled inheritance. By knowing when and how to use these concepts, developers can write cleaner, maintainable, and more efficient code.
No, a class cannot be both abstract and sealed because abstract classes are designed for inheritance, while sealed classes prevent it.
Yes, a sealed class can implement one or more interfaces. Only inheritance from another class is restricted.
Yes, abstract classes can have constructors, which are called when a derived class is instantiated.
No, sealed classes cannot be inherited, either directly or indirectly, in C#.
Use an abstract class when you want to share code among related classes and provide common functionality. Use an interface when you want to define a contract without implementation.
Copyrights © 2024 letsupdateskills All rights reserved