Abstract Class in C#
Abstract class
An abstract class is a class that cannot be used to create objects. It is intended to be inherited by derived classes that provide implementations for its abstract members.
The abstract keyword is used to define a class, method, property, indexer, or event with incomplete implementation, which must be completed in a derived class.
¨ Abstract Method:
o An abstract method is declared without a body and only inside an abstract class.
o It must be implemented in any non-abstract class that inherits the abstract class, using the override keyword.
Important Points:
- Abstract classes are mainly used with inheritance.
- An abstract class can have both abstract and non-abstract methods.
- Abstract methods must be implemented in the child class using the override keyword.
- It can have constructors and destructors.
- It can include normal methods with full implementation.
- It does not support multiple inheritance.
- It cannot be declared as static.
- It can have properties with get and set accessors.
❓ Why does this fail?
👉 Method access depends on the reference type, not the object type.
Reference type:
BaseSumClassBaseSumClassdoes NOT containShowDerivedMessage()
✔ Even though the object is DerivedSumClass, the compiler checks only the reference type.
Note :
<> Method accessibility is determined by reference type,
<> But method execution (overriding) is determined by object type.
Now inherit this class in the derived class
If I don’t want to use a child class constructor, can I pass values directly to the parent class constructor?
🔴 Why Is This Not Possible?
An object of a derived class cannot be created without first running a derived class constructor.
Even if we don’t write one explicitly, C# creates it implicitly.
You cannot pass parameters to a base class constructor without going through a derived class constructor. Constructor chaining is mandatory in C# inheritance.
🔑 Remember This Sentence
No child object without a child constructor.
C# 14 Abstract Classes & Inheritance
Extension Members for Abstract Classes
- You can now add new methods, properties, and events to an abstract class without editing its source code.
- This is great for adding extra functionality to base classes from external libraries.
Extension Operators
- You can define operators (
+,-,*,/, etc.) for abstract classes outside the class itself. - Makes it possible to combine or compare abstract class objects in a clean way.
- You can define operators (
fieldKeyword for Backing Fields- When overriding or implementing properties in an abstract class, you can now use the
fieldkeyword to directly reference the compiler-generated backing field. - This reduces boilerplate code in property overrides.
- When overriding or implementing properties in an abstract class, you can now use the
Null-Conditional Assignment (
??=) in Abstract Members- You can now safely initialize nullable fields or properties in abstract classes using
??=without extra null checks.
- You can now safely initialize nullable fields or properties in abstract classes using
Improved Pattern Matching for Abstract Types
- Pattern matching now works better with abstract classes, allowing type-specific matching without extra casting.
Primary Constructors in Abstract Classes
- Abstract classes can now have primary constructors (like records), making it easier to pass parameters to base classes.
Please let me know, if I need to do more enhancement in it
ReplyDelete