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:

  1. Abstract classes are mainly used with inheritance.
  2. An abstract class can have both abstract and non-abstract methods.
  3. Abstract methods must be implemented in the child class using the override keyword.
  4. It can have constructors and destructors.
  5. It can include normal methods with full implementation.
  6. It does not support multiple inheritance.
  7. It cannot be declared as static.
  8. 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: BaseSumClass

  • BaseSumClass does NOT contain ShowDerivedMessage()

✔ 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?

In C#, creating a derived object always starts with the derived class constructor, which then calls the base class constructor. You cannot bypass the derived constructor. We must create derived class constructor if base class constructor is parameterized.

🔴 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.

🧠 Interview answer (Constructor Chaining)
You cannot pass parameters to a base class constructor without going through a derived class constructor. Constructor chaining is mandatory in C# inheritance.

In simple word: Whenever you create an object of a child (derived) class, its constructor MUST run first. 

🔑 Remember This Sentence

No child object without a child constructor.













C# 14 Abstract Classes & Inheritance

  1. 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.
  2. 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.
  3. field Keyword for Backing Fields

    • When overriding or implementing properties in an abstract class, you can now use the field keyword to directly reference the compiler-generated backing field.
    • This reduces boilerplate code in property overrides.
  4. Null-Conditional Assignment (??=) in Abstract Members

    • You can now safely initialize nullable fields or properties in abstract classes using ??= without extra null checks.
  5. Improved Pattern Matching for Abstract Types

    • Pattern matching now works better with abstract classes, allowing type-specific matching without extra casting.
  6. Primary Constructors in Abstract Classes

    • Abstract classes can now have primary constructors (like records), making it easier to pass parameters to base classes.

Comments

Post a Comment