Abstract classes Error 1

The abstract modifier is used to represents incomplete implementations in C#.

The abstract modifier is applicable to classes, methods, properties, indexers and events. Abstract classes and abstract methods are used in inheritance to provide generalized functionalities to derived classes.

Abstract Method:

The abstract method contains no body. It is mandatory for derived classes to override abstract methods of the base class and provide the full implementation of these methods.

The general syntax of the abstract method is as below:

public abstract return_type <method_name>(argument list);

Look at syntax there is no method body after argument list; after the argument, list statement is terminated with the semicolon.

All abstract methods must be declared as public otherwise compiler throws the error.

To override abstract methods in derived classes override keyword is used as below:

public override return_type<method_name>( argument list)
{ 
  // Statements to be executed
}

You cannot declare the static method as abstract. A class contains any number of abstract methods.

Once you add abstract method in class then it is mandatory to declare that class as abstract class.

Abstract Class:

An abstract class is declared with abstract keyword. As abstract classes are not fully defined, so you cannot create an object of abstract class.

The purpose of the abstract class is to provide some generalized functionalities that must be define by derived classes.

Abstract class contains concrete (non abstract) and abstract methods. Abstract classes are not fully defined therefore we cannot create any object of it.

The general form of abstract class is as below:

abstract class <class_name>
{
  //Abstract methods
  //Non abstract methods
}

Example:

using System;
namespace AbstractClassDemo1
{
class Program
{
abstract class AbstractBaseClass
{
public abstract void Display();
public void Show()
{
Console.WriteLine("Base class show method executed.");
}
}
class DerivedClass1 : AbstractBaseClass
{
public override void Display()
{
Console.WriteLine("Display method is implemented in derived class 1.");
}
public void ShowDerived()
{
Console.WriteLine("Derived class show method executed.");
}
}
class DerivedClass2 : AbstractBaseClass
{
public override void Display()
{
Console.WriteLine("Display method is implemented in derived class 2.");
}
}
static void Main(string[] args)
{
DerivedClass1 derivedClass1 = new DerivedClass1();
derivedClass1.Display();
DerivedClass2 derivedClass2 = new DerivedClass2();
derivedClass2.Display();
Console.ReadLine();
}
}
}

The output of above program is:

The display method is implemented in derived class 1.
The display method is implemented in derived class 2.

In above program, we created one abstract method Display() in class AbstractBaseClass, so it is necessary to declare that class as abstract. If AbstractBaseClass is not declared as abstract then compiler throws following error.

Recommended Post   C# Classes & Objects 

Abstract Class Error

DerivedClass1 and DerivedClass2 are inherited from class AbstractBaseClass, so these two classes must override the method Display().

Some points to be remembered about the abstract class:

  1. Abstract classes are not fully defined, so you cannot create the object of the abstract class with the new operator. If you try to create an object of an abstract class, it will result in following.Abstract classes Error 1
  2. You cannot create an object of abstract class but you can create a reference variable of an abstract class, which refers to the object of derived class like below:
AbstractBaseClass  derivedClass1 = new DerivedClass1();

With the help of this reference variable, we can invoke all the nonabstract methods that are inherited in derived class as well as the abstract methods, but any pure members of derived class are not accessible to this reference variable. In above example, if we change the code in the main method as below:

static void Main(string[] args)
{
AbstractBaseClass derivedClass1 = new DerivedClass1();
derivedClass1.Display();
derivedClass1.Show();
//derivedClass1.ShowDerived() ;   ShowDerived() not accessible
Console.ReadLine();
}

The output is:

The display method is implemented in derived class 1.
Base class show method executed.

Here we can access base class’s Show () method. But we cannot access ShowDerived() method because this method is introduced in derived class and it is not a part of the base class so we cannot access this method with the reference variable of the base class.

  1. If a class contains one or more abstract methods then it must be declared as abstract.
  2. If a class is derived from abstract class, it must override all abstract methods of the base class. If derived class does not give an implementation of abstract methods then it must be declared as abstract. This abstract attribute is inherited until all abstracts methods are fully implemented in the class hierarchy.
Recommended Post   Anonymous Classes and Methods in C#

Example:

using System;
namespace AbstractClassDemo1
{
class Program
{
abstract class A
{
public abstract void Display();
}
abstract class B : A
{
}
class C : B
{
public override void Display()
{
Console.WriteLine("Display method is implemented in class C.");
}
}
static void Main(string[] args)
{
C c = new C();
c.Display();
Console.ReadLine();
}
}
}

The output of above program is:

The display method is implemented in class C.

In above program, abstract class A has Displayed() abstract method. When we inherit this class in class B we don’t override Display() method, so we declare that class as abstract. When class C is inherited from class C we must override the Display() method.

If don’t override this method in class C then again we have to declare that class as abstract class. This chain of an abstract class is going on until the full implementation of Display() method is given by derived class.

  1. An abstract class cannot be declared as sealed class.

Need of Abstract class:

Now let’s learn why these abstract classes and methods are used in the class hierarchy. Suppose there is a situation where we want to that all derived classes to define some common functionality.

In this situation, abstract methods and abstract class are used. In real life, abstract methods and abstract classes are useful while developing reusable code frameworks.

Suppose there are some classes of geometrical shape and we want that all these classes must provide the method that calculates the area of the shape.

If we create the base class with the method which calculates the area of shapes then all derived classes can inherit this shape method but the problem is that the code to calculate the area of different shapes is different.

Recommended Post   C# OOPS

To overcome this problem we make the method abstract in the base class which enforces all derived classes to override this method provide their own version of this method to calculate the area. Let’s see following example to calculate the area of square and circle shape.

Example:

using System;
namespace AbstractClassDemo
{abstract class ShapeArea
abstract class ShapeArea
{
public abstract double ShowArea();
}
class SquareShape : ShapeArea
{
public double length;
public override double ShowArea()
{
double area = 0;
area = length * length;
return area;
}
}
class CircleShape : ShapeArea
{
const double PI = 3.14;
public double radius;
public override double ShowArea()
{
double area = 0;
area = PI * (radius * radius);
return area;
}
}
class Program
{
static void Main(string[] args)
{
SquareShape squareShape = new SquareShape();
squareShape.length = 12;
Console.WriteLine("Area of square is: {0}", squareShape.ShowArea());
CircleShape circleShape = new CircleShape();
circleShape.radius = 5;
Console.WriteLine("Area of circle is: {0}", circleShape.ShowArea());
Console.ReadLine();
}
}
}

The output of above program is:

Area of square is: 144
Area of circle is: 78.5

To enforce derived classes to provide the method to calculate area we create an abstract base class ShapeArea and in this class ShowArea() method is declared as an abstract class.

Now we create two classes SquareArea and CircleArea, these two classes inherits from base class ShapeArea, so it is mandatory to override ShowArea() method in these two classes. In this method, we write area calculation code respective to that shape.

In this way we enforce all derived classes of ShapeArea class to provide ShowArea() method.

Recommended Reading Source:
http://www.c-sharpcorner.com/UploadFile/93126e/importance-and-use-of-versioning-in-C-Sharp/