Abstract Class And Abstract Methods in C#
The Abstract class is a class can contain both Abstract and Non-Abstract Method, to consume the non-Abstract method available in the abstract class then it is required or mandate to implement the abstract method of abstract Class.
Abstract Methods à Defining the declaration of method I.e. Method without method body and the implementation of this method is available in the child class.
Example,
abstract public class ParentClass// Parent Class
{
public abstract void Display();
public void NormalMethod()
{
MessageBox.Show("Abstract Class can also have the normal Method");
}
}
Non-Abstract Methods à Defining the method with Method Body I.e. Normal method
Example,
public class OverridingAbstractClass : ParentClass// Child Class Inherting the Parent Class
{
//
public override void Display() // Overridden in the Child Class with the
implementing in the child Class - Mandate for Abstract Class
{
MessageBox.Show($"{this.GetType().Name} - Displayed from OverridingClass Method");
}
}
The process of implementing the method defined in the parent class in the child Class and These Methods are defined with the Modifier ABSTRACT in the parent Class and Overridden in the child Class with the Modifier OVERRIDE.
We can Directly use non abstract method defined in the parent class in the child Class, but it is mandated to implement the method body of abstract method in the child class. When a Method is defined as ABSTRACT In a class then we are giving an Option to Implement the method in the child class or inherited Class These are mainly derived from the Base library – “System” Class.
Example,
Here in This Example,
Parentclass Has Methods – Such as “Display (Abstract Method) and NormalMethod (Non-Abstract Method)” – With no parameter available, and the Abstract defined with the abstract modifier, hence the Method is required to Implement the method body in the child class.
OverrdingAbstractClass à In Our Application, another class has been defined with the Name “Overriding abstract Class” but this class want the same method defined in the parent class – so we have inherited in the child class.
At the Same time, behavior is not implemented in the child class, so user has Implemented the method as required by using the modifier Override
Comments
Post a Comment