Introduction
Requirement
2) Explain Abstract Classes in C# with Simple Example.
3) Explain Abstract Methods in C#.
4) Explain the Difference between Abstract Class and Interface in C#.
Implementation
What is Abstraction in C#?
Abstract Class
Important Points of Abstract Class
- Abstract class can't be instantiated.
- It is used at the time of inheritance.
- The abstract class can contain abstract methods, assessors, non-abstract methods, properties, and indexers.
- In C# It can't be inherited by structures.
- The abstract class can contain constructors or destructors
- The abstract class can implement functions with non-Abstract methods
- Abstract class can't be static.
- An abstract class does not support multiple inheritances
- In an abstract class, if you declared a class as an abstract class, then you can't create an instance/object for that class.
- When your class contains a minimum of one abstract method, then you need to declare that class as an abstract class.
The syntax of Abstract Class
modifier abstract class YourClassName
{
// Application Logic...
}
Abstract Methods
The syntax of Abstract Methods
modifier abstract class YourClassName
{
//declare fields (which can contain assignments)
modifier dataType YourVariableName;
//declare your methods
modifier abstract dataType YourMethodName();
}
modifier class childClass : YourClassName
{
override modifier dataType YourMethodName(){}
}
Example of the Abstract Class and Abstract method
using System;
// you have to declare class 'AreaClass' as abstract
abstract class AreaClass
{
// You have to declare method 'Area' as abstract
abstract public int Area();
}
// You have to inherit class 'AreaClass' in child class 'Square'
class Square: AreaClass
{
int side = 0;
// Create constructor
public Square(int n)
{
side = n;
}
// You have to Create the abstract method 'Area' is overridden here
public override int Area()
{
return side * side;
}
}
class MyClass{
// Your Main Method
public static void Main()
{
Square s = new Square(4);
Console.WriteLine("Area of a Square = " + s.Area());
}
}
Output
Area of a Square = 16
Difference between Abstract Class and Interface in C#
- Abstract Class contains both the declaration and definition part while Interface contains only a declaration part.
- Abstract Class does not support Multiple Inheritance while Interface supports Multiple Inheritance.
- Abstract Class Contains Data members while Interface does not contains Data Member.
- Abstract Class Contain constructor While Interface does not contain constructor.
- Abstract Class Contain static members While Interface does not contain static members.
- Abstract Class Can Contain Access Modifiers While Interface does not contain Access Modifiers and by default, everything assume as public.
- Abstract Class Can Provide Complete Implementation while Interface only has signature not Implementation.
- Abstract Class is fast compared to Interface.
- An abstract class can have defined fields and constants while Interface cannot have fields.
- An abstract class used to implement the core identity of the class while Interface is used to implement the peripheral abilities of the class.