Tech Point Fundamentals

Tuesday, November 10, 2020

Abstract Class and Abstract Method C#

 Abstract Class and Abstract Method C#

AbstractClass-And-AbstractMethod-InCSharp

  Data Abstraction is the process of hiding certain details and showing only essential information to the user. In C# abstraction can be achieved with either abstract classes or interfaces. In this article, we will walk through the abstract class and methods in detail.




Introduction

The abstract modifier indicates that the type being modified has a missing or incomplete implementation. In C# the abstract modifier can be used with classes, methods, properties, indexers, and events for making them abstract.

Abstract Class

The purpose of an abstract class is to provide a skeletal structure or blueprint for other classes to derive from along with the common reusable methods, properties, events, or indexers.

An abstract class may or may not have any abstract method but, it can never be instantiated. It can only be used as a base class, and the inheriting class must have to implement the abstract members of the base class unless the inheriting class is also an abstract class.

Abstract Class Fundamental Points

    1. An abstract class can implement interfaces.
    2. Abstract classes can also inherit from other abstract classes or normal classes.
    3. An abstract class can exist without any abstract method at all. 
    4. If a class has any abstract member, then the class must be abstract otherwise the compiler generates a compile-time error.
    5. Abstract classes can have constructors and destructors.
    6. An abstract class can also have sealed methods as well.
    7. An abstract class can contain both static and instance variables.
    8. Abstract classes can have virtual and concrete methods as well.
 


Abstract Class Fundamental Rules

    1. An abstract class can never be instantiated, but its reference can be created
    2. An abstract class cannot be static.
    3. An abstract class cannot be sealed because both the modifiers have opposite meanings but it can contain sealed methods.
    4. An abstract class cannot be inherited by structures.
    5. Abstract classes cannot inherit from structures.
    6. The non-abstract classes cannot have any abstract members.
    7. An abstract class that implements an interface might map the interface methods onto abstract methods.
    8. Abstract classes cannot be used for achieving multiple inheritances in C#.
    9. An abstract class can override an abstract member as abstract again. 
    10. An abstract class can override the virtual member as abstract also that is known as re-abstraction. 
    11. Abstract classes can redefine the concrete member of the base class as abstract also by new keyword i.e. known as shadowing.

Why Abstract Class

In C# to achieve abstraction and security, we use abstract classes. Normally we use the abstract classes when we create frameworks for the project. If you see the .Net frameworks you can find a lot of abstract classes. 

Normally we must have to declare the class as abstract if:
    1. The class contains any abstract members.
    2. The class does not provide implementation to any of the methods of the inherited interface.
    3. The class does not provide the implementation of the base class abstract methods.



Example


 

Abstract Class Vs Interface

Please read about the Abstract Class Vs Interface here.

Abstract Method

A method without body definition and having an "abstract" modifier is known as an abstract method. The keyword "abstract" can only be used just before the return type of the method.

Before C# 8 only abstract classes can have abstract methods, but with the introduction of C# 8 interfaces can also have abstract members. 



Please read more about the interface abstract method here.

The inheriting class must have to implement all the abstract members with the "override"  keyword unless the inheriting class is also an abstract class because all the abstract methods are virtual implicitly.

If you see the IL code of the abstract method of the above example in "ILDASM":
AbstractMethod-ILCode
Click on image to see full view

Abstract Method Fundamental Points

    1. An abstract method cannot have a body definition.
    2. The "abstract" keyword must be used before the return type of the method.
    3. The access modifier of the abstract method should be the same in both the abstract class and its derived class.
    4. All the abstract methods are virtual implicitly by default.
    5. The inheriting class must have to implement the abstract members with an override keyword because an abstract method is virtual implicitly.



Abstract Method Fundamental Rules

    1. An abstract method cannot be private.
    2. Only an abstract class can have abstract members.
    3. An interface can also have an explicit abstract method with the introduction of C# 8.
    4. Abstract members cannot have the "virtual" modifier because all the abstract members are implicitly virtual.
    5. An abstract method cannot be sealed but it can be overridden as sealed in the derived class.
    6. An abstract method cannot be marked as static.

Why Abstract Methods

We normally make a method as abstract:
    1. If two or more than two child classes are expected to implement the same functionality but in a different manner based on the requirement.
    2. When we do not want to implement all the inheriting interface methods.
    3. When we want to achieve re-abstraction.

Abstract Method Vs Virtual Method

    1. An abstract method cannot contain body definition while a virtual method must have a body definition.
    2. Overriding all the abstract methods in the inheriting concrete class is mandatory while overriding the virtual methods is optional.
    3. The "abstract" modifier can be used while overriding the abstract method but the "virtual" modifier cannot be used while overriding a virtual method.
    4. An abstract method can only be declared in an abstract class while a virtual method can be defined in a non-abstract class as well.
    5. A virtual method can be overridden to abstract for achieving re-abstraction but an abstract method cannot be overridden to virtual.



Abstract Property

We can also declare a property as abstract in C#. An abstract property declaration does not provide an implementation of the property accessors. 

Abstract properties behave like abstract methods, except for the differences in the declaration and invocation syntax. An abstract property can be overridden in the derived class.

Real Examples of Abstract Class and Methods

    1. System.Type is an abstract class having both abstract methods and abstract properties. 
    2. System.Enum is an abstract class without any abstract member.
    3. System.ValueType is an abstract class.
    4. System.Reflection.Assembly is an abstract class without any abstract member.
    5. GUID, Assembly, FullName, Namespace, BaseType, Module, UnderlyingSystemType are abstract properties available in System.Type abstract class.
    6. GetConstructors(), GetElementType(), GetEvent(), GetField(), GetInterface(), GetMembers(), GetMethods(), GetProperties() are abstract methods available in System.Type abstract class.
    7. All the static classes are abstract implicitly by default.

Live Demo


No comments:

Post a Comment

Please do not enter any HTML. JavaScript or spam link in the comment box.