Abstract and Sealed Classes and Class Members - C# (2024)

  • Article

The abstract keyword enables you to create classes and class members that are incomplete and must be implemented in a derived class.

The sealed keyword enables you to prevent the inheritance of a class or certain class members that were previously marked virtual.

Abstract Classes and Class Members

Classes can be declared as abstract by putting the keyword abstract before the class definition. For example:

public abstract class A{ // Class members here.}

An abstract class cannot be instantiated. The purpose of an abstract class is to provide a common definition of a base class that multiple derived classes can share. For example, a class library may define an abstract class that is used as a parameter to many of its functions, and require programmers using that library to provide their own implementation of the class by creating a derived class.

Abstract classes may also define abstract methods. This is accomplished by adding the keyword abstract before the return type of the method. For example:

public abstract class A{ public abstract void DoWork(int i);}

Abstract methods have no implementation, so the method definition is followed by a semicolon instead of a normal method block. Derived classes of the abstract class must implement all abstract methods. When an abstract class inherits a virtual method from a base class, the abstract class can override the virtual method with an abstract method. For example:

// compile with: -target:librarypublic class D{ public virtual void DoWork(int i) { // Original implementation. }}public abstract class E : D{ public abstract override void DoWork(int i);}public class F : E{ public override void DoWork(int i) { // New implementation. }}

If a virtual method is declared abstract, it is still virtual to any class inheriting from the abstract class. A class inheriting an abstract method cannot access the original implementation of the method—in the previous example, DoWork on class F cannot call DoWork on class D. In this way, an abstract class can force derived classes to provide new method implementations for virtual methods.

Sealed Classes and Class Members

Classes can be declared as sealed by putting the keyword sealed before the class definition. For example:

public sealed class D{ // Class members here.}

A sealed class cannot be used as a base class. For this reason, it cannot also be an abstract class. Sealed classes prevent derivation. Because they can never be used as a base class, some run-time optimizations can make calling sealed class members slightly faster.

A method, indexer, property, or event, on a derived class that is overriding a virtual member of the base class can declare that member as sealed. This negates the virtual aspect of the member for any further derived class. This is accomplished by putting the sealed keyword before the override keyword in the class member declaration. For example:

public class D : C{ public sealed override void DoWork() { }}

See also

Abstract and Sealed Classes and Class Members - C# (2024)
Top Articles
BSE listed companies' market cap hits $5 trillion for first time
10 things to know about ESG and sustainable investing | ESG Truths
9.4: Resonance Lewis Structures
Truist Bank Near Here
Splunk Stats Count By Hour
Shoe Game Lit Svg
Craftsman M230 Lawn Mower Oil Change
What to Do For Dog Upset Stomach
Workday Latech Edu
Undergraduate Programs | Webster Vienna
Ou Class Nav
Walgreens On Nacogdoches And O'connor
Washington, D.C. - Capital, Founding, Monumental
Help with Choosing Parts
Nalley Tartar Sauce
De beste uitvaartdiensten die goede rituele diensten aanbieden voor de laatste rituelen
Watch The Lovely Bones Online Free 123Movies
Wausau Marketplace
Vigoro Mulch Safe For Dogs
Culver's Flavor Of The Day Taylor Dr
Busted News Bowie County
Sef2 Lewis Structure
Naya Padkar Gujarati News Paper
Why Are Fuel Leaks A Problem Aceable
Craig Woolard Net Worth
Criterion Dryer Review
10 Best Places to Go and Things to Know for a Trip to the Hickory M...
Ardie From Something Was Wrong Podcast
Pacman Video Guatemala
Abga Gestation Calculator
Our 10 Best Selfcleaningcatlitterbox in the US - September 2024
Sinfuldeed Leaked
Angel del Villar Net Worth | Wife
Evil Dead Rise - Everything You Need To Know
Shaman's Path Puzzle
Of An Age Showtimes Near Alamo Drafthouse Sloans Lake
Adecco Check Stubs
Vitals, jeden Tag besser | Vitals Nahrungsergänzungsmittel
The Mad Merchant Wow
Western Gold Gateway
Games R Us Dallas
Property Skipper Bermuda
Сталь aisi 310s российский аналог
Bob And Jeff's Monticello Fl
Mississippi weather man flees studio during tornado - video
VDJdb in 2019: database extension, new analysis infrastructure and a T-cell receptor motif compendium
How to Install JDownloader 2 on Your Synology NAS
Matt Brickman Wikipedia
Spn 3464 Engine Throttle Actuator 1 Control Command
Houston Primary Care Byron Ga
OSF OnCall Urgent Care treats minor illnesses and injuries
Itsleaa
Latest Posts
Article information

Author: Chrissy Homenick

Last Updated:

Views: 5897

Rating: 4.3 / 5 (54 voted)

Reviews: 93% of readers found this page helpful

Author information

Name: Chrissy Homenick

Birthday: 2001-10-22

Address: 611 Kuhn Oval, Feltonbury, NY 02783-3818

Phone: +96619177651654

Job: Mining Representative

Hobby: amateur radio, Sculling, Knife making, Gardening, Watching movies, Gunsmithing, Video gaming

Introduction: My name is Chrissy Homenick, I am a tender, funny, determined, tender, glorious, fancy, enthusiastic person who loves writing and wants to share my knowledge and understanding with you.