Introduction to Abstract Class and Abstract Method In C#

watch_later Saturday, February 9, 2019

Introduction


In this article, I am going to explain some of the basic information about the abstract class, abstract method, and abstract property in c# as well as some differences between abstract class and Interface in C# and explains what is an abstraction in C#. Here I also explain how to use an abstract class, method, and abstract property in c# with a simple example.

Abstract Class and Abstract Method In C#

Many of developers/programmers those who work with C# or VB.NET will have at least heard talk about the abstract class, abstract method, an abstract property. Even if any developers/programmers know on a basic level abstract class, abstract method, an abstract property do, they are not always certain when to use an abstract class, abstract method, an abstract property and how to create the code to create an abstract class, and abstract method or abstract property So, here I will share some basic information regarding it.

Requirement


1) What is Abstraction in C#?
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#?


In C# abstraction is the process to hide the internal details/information and showing only the required functionality.

Abstract Class


The abstract class is a special kind of class and that can't be instantiated and it is to achieve the abstraction in C#. Generally, it is used at the time of inheritance. It doesn't not only contain abstract methods and assessors however it contains non-abstract methods, properties, and indexers additionally. The abstract class cannot be inheritable by structures and. It will contain constructors or destructors and may implement functions with non-Abstract methods. Abstract class can't be static and cannot support multiple inheritance. When you declared a class as an abstract class, then you can not create an instance for that class. however, you can be used as a parameter in a method.

Important Points of Abstract Class


1) Abstract class can't be instantiated.
2) It is used at the time of inheritance.
3) The abstract class can contain abstract methods, assessors, non-abstract methods, properties, and indexers.
4) In C# It can't be inherited by structures.
5) The abstract class can contain constructors or destructors
6) The abstract class can implement functions with non-Abstract methods
7) Abstract class can't be static.
8) An abstract class does not support multiple inheritances
9) In an abstract class, if you declared a class as an abstract class, then you can't create an instance/object for that class.
10) 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


Abstract Method is similar to methods within an interface which is declared abstract has no “body” and declared inside the abstract class only in short Abstract Method are declared without any implementation. Abstract Methods are declared with the purpose of having the child class provide implementation and you have to make sure abstract methods must be declared within an abstract class.

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


Here, we will take an example to calculate the area of a square using an abstract class and an 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 SquareAreaClass
{
	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#


1) Abstract Class contains both the declaration and definition part while Interface contains only a declaration part.
2) Abstract Class does not support Multiple Inheritance while Interface supports Multiple Inheritance.
3) Abstract Class Contains Data members while Interface does not contains Data Member.
4) Abstract Class Contain constructor While Interface does not contain constructor.
5) Abstract Class Contain static members While Interface does not contain static members.
6) Abstract Class Can Contain Access Modifiers While Interface does not contain Access Modifiers and by default, everything assume as public.
7) Abstract Class Can Provide Complete Implementation while Interface only has signature not Implementation.
8)  Abstract Class is fast compared to Interface.
9) An abstract class can have defined fields and constants while Interface cannot have fields.
10) An abstract class used to implement the core identity of the class while Interface is used to implement the peripheral abilities of the class.

Summary


In this article, we saw what is abstract classes, abstract methods, and abstract properties with its syntax and simple example and as well as also saw some basic differences between an interface in C#. I hope this article will help you. Thank you.

Codingvila provides articles and blogs on web and software development for beginners as well as free Academic projects for final year students in Asp.Net, MVC, C#, Vb.Net, SQL Server, Angular Js, Android, PHP, Java, Python, Desktop Software Application and etc.



sentiment_satisfied Emoticon