Abstract classes in C#

Now we are going to learn about the Abstract classes in C#.
  • 3459

Introduction

In the Abstract classes we use the word abstract before defining the class. In abstract classes we will create the class and class members until we will not create the derived class. The purpose to create the abstract class is that the multiple class can share conman definition abstracts classes.

An abstract class can hold abstract method or non abstract methods. Abstract classes can defined the abstract methods.

Abstract classes are mostly related to the interface .There is a class name "car" the another subclasses like the named Dezier,Eon, Alto is the derived classes that are access the property of the base classes it is called the abstract classes.

There are slightly logical difference between abstract classes and the interface. Interface class does not contain any concrete method but abstract classes can contain methods.  Multiple inheritance is not possible in the abstract classes but it is possible in interface.

Example of the abstract class:

abstract  class shape1
{
protected float R, L, B;
public abstract float Area();
public abstract float Circumference()
}
class Rectangle1:shape1
{
public void getlb()
{
Console.Write("enter length: ");
L = float.Parse(Console.ReadLine());
Console.Write("enter Breadth: ");
B = float.Parse(Console.ReadLine());
}
public override float Area()
{
return L * B;
}
public override float Circumference()
{
return 2*(L+B);
}
}
class circle1:shape1
{
public void getradius()
{
Console.Write("enter radius : ");
R = float.Parse(Console.ReadLine());
}
public override float Area()
{
return 3.14f * R * R;
}
public override float Circumference()
{
return 2 * 3.14f * R;
}
}
class MainClass
{
public static void calculate(shape1 s)
{
Console.WriteLine ("area:{0}",s.Area ());
Console.WriteLine ("circumferfence : {0}" ,s.Circumference ());
}
static void Main()
{
Rectangle1 R = new Rectangle1();
R.getlb();
calculate(R);
Console.WriteLine();
circle1 C = new circle1();
C.getradius();

calculate(C);
Console.Read();
}
}

Abstract Class

1.It can contain concrete methods(methods with implementation). So, in other words abstract class can contain methods with both implementation
and without implementation. 
2. Multiple inheritance is not possible in case of abstract class. 
3. Access Specifiers are been supported in abstract class. 

Interface Class

1. Does not contain any concrete methods. 
2. Multiple Inheritance is possible with interface.
3. Access Specifiers are not supported in Interface.  

 

© 2020 DotNetHeaven. All rights reserved.