Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Abstract vs Sealed Classes vs Class Members in C#
The abstract class includes abstract and non-abstract methods. You cannot instantiate an abstract class directly. The sealed class prevents inheritance and you cannot use it as a base class. Both abstract and sealed classes can contain various types of class members with different access modifiers and behaviors.
Abstract Classes
To declare an abstract class, you need to place the keyword abstract before the class definition. An abstract class can contain abstract methods that must be implemented by derived classes −
Syntax
public abstract class ClassName {
public abstract void MethodName();
public void RegularMethod() {
// Non-abstract method with implementation
}
}
Example
using System;
public abstract class Vehicle {
public abstract void display();
public void StartEngine() {
Console.WriteLine("Engine started");
}
}
public class Car : Vehicle {
public override void display() {
Console.WriteLine("This is a Car");
}
}
public class Bike : Vehicle {
public override void display() {
Console.WriteLine("This is a Bike");
}
}
public class Program {
public static void Main() {
Vehicle car = new Car();
Vehicle bike = new Bike();
car.display();
car.StartEngine();
bike.display();
bike.StartEngine();
}
}
The output of the above code is −
This is a Car Engine started This is a Bike Engine started
Sealed Classes
To declare a sealed class, you need to place the keyword sealed before the class definition. The sealed class prevents inheritance and you cannot use it as a base class −
Syntax
public sealed class ClassName {
// Class members comes here
}
Example
using System;
public sealed class MathHelper {
public static double CalculateCircleArea(double radius) {
return Math.PI * radius * radius;
}
public static double CalculateSquareArea(double side) {
return side * side;
}
}
public class Program {
public static void Main() {
double circleArea = MathHelper.CalculateCircleArea(5);
double squareArea = MathHelper.CalculateSquareArea(4);
Console.WriteLine("Circle Area: " + circleArea);
Console.WriteLine("Square Area: " + squareArea);
}
}
The output of the above code is −
Circle Area: 78.5398163397448 Square Area: 16
Class Members in Abstract and Sealed Classes
Both abstract and sealed classes can contain various types of class members including fields, properties, methods, and constructors. Here's an example demonstrating different class members −
Example
using System;
public abstract class Animal {
protected string name;
public int age;
public Animal(string name, int age) {
this.name = name;
this.age = age;
}
public abstract void MakeSound();
public void Sleep() {
Console.WriteLine(name + " is sleeping");
}
}
public sealed class Dog : Animal {
public Dog(string name, int age) : base(name, age) {
}
public override void MakeSound() {
Console.WriteLine(name + " says Woof!");
}
public void Fetch() {
Console.WriteLine(name + " is fetching the ball");
}
}
public class Program {
public static void Main() {
Dog myDog = new Dog("Buddy", 3);
myDog.MakeSound();
myDog.Sleep();
myDog.Fetch();
Console.WriteLine("Age: " + myDog.age);
}
}
The output of the above code is −
Buddy says Woof! Buddy is sleeping Buddy is fetching the ball Age: 3
Comparison
| Feature | Abstract Class | Sealed Class |
|---|---|---|
| Instantiation | Cannot be instantiated | Can be instantiated |
| Inheritance | Must be inherited | Cannot be inherited |
| Abstract Methods | Can have abstract methods | Cannot have abstract methods |
| Purpose | Provide base structure | Prevent further inheritance |
Conclusion
Abstract classes provide a foundation for inheritance with abstract methods that derived classes must implement, while sealed classes prevent inheritance entirely. Both can contain various class members like fields, properties, and methods to encapsulate data and behavior effectively.
