Friday, 1 May 2015

What is an Abstract class in c# ?

Here, first am expaling the comcept of Abstract class
Abstract classes, marked by the keyword abstract in the class definition, are typically used to define a base class in the hierarchy. What's special about them, is that you can't create an instance of them - if you try, you will get a compile error. Instead, you have to subclass them, as taught in the chapter on inheritance, and create an instance of your subclass. So when do you need an abstract class? It really depends on what you do.

Why we need to define Abstract classes ?
The purpose of abstract class is to provide default functionality to its sub classes.
When a method is declared as abstract in the base class then every derived class of that class must provide its own definition for that method.
 An abstract class can also contain methods with complete implementation, besides abstract methods.
When a class contains at least one abstract method, then the class must be declared as abstract class.
It is mandatory to override abstract method in the derived class.
When a class is declared as abstract class, then it is not possible to create an instance for that class. But it can be used as a parameter in a method.

The  example below  creates three classes called Calculation1, Circle and Rectangle where circle and rectangle are inherited from the class Calculation1and implements the methods getArea() using override keyword and call the base class methods getCircle_Circumference() and geRectangle_Circumference that are implemented in abstract class Calculation1 itself and as Calculation1 class contains abstract methods so it is declared as abstract class.


Source code example in c# 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Globalization;
using System.Data;

namespace ConsoleDemoApp
{
    class Test
    {
        /* program to write a exception in c# */

        Public static void Main(string[] args)
        {
            Double length, width, radius;

           
            Console.Write("\nEnter Length of a Rectangle: ");
            length = Convert.ToDouble(Console.ReadLine());
            Console.Write("\nEnter Width of a Rectangle: ");
            width = Convert.ToDouble(Console.ReadLine());
            Console.Write("\nEnter the Radius of Circle : ");
            radius = Convert.ToDouble(Console.ReadLine());

            /* Class Rectangle */
            Rectangle rec = new Rectangle(length, width);
            // getting Area rectangle
            Double area = rec.getArea();
            Console.Write("\nArea of a Rectangle is: {0}\n", area);
            // getting Circumference
            Double Circum = rec.getCircum();
            Console.Write("\nCircumference of Rectanle is: {0}\n", Circum);
          

            /* Class Circle */
            Circle cl = new Circle(radius);
            // getting Area of circle
            Double CircleArea = cl.getArea();
            Console.Write("\nArea of Circle is: {0}\n", CircleArea);

            Double CircleCircum = cl.getCircum();
            Console.Write("\nCircumference of Circle is: {0}\n", CircleArea);

            Console.ReadKey();
        }
    }

    Public abstract class Calculation1
    {
      Public Double length, width, radius;

        // abstract function it must be override in derived class
        Public abstract Double getArea();

        // Common function 1
        Public  Double getCircle_Circumference(Double Radius)
        {
            Double result = 2 * 3.14 * Radius;
            return result;
        }

        // Common function 2
        Public Double geRectangle_Circumference(Double length, Double width)
        {
            Double result = 2 * (length * width);
            Return result;
        }

    }

    Public class Circle : Calculation1
    {
        // constructor method to asign calss variables
        Public Circle(Double Radius)
        {
            this.redius = Radius;
        }

        // overriding the base class function
        Public override Double getArea()
        {
            Double area = 3.14 * this.redius * this.redius;
            return area;
        }

        // calling base class method
        Public Double getCircum()
        {
            Double circumference = this.getCircle_Circumference(this.redius);
            Return circumference;
        }
       
    }

    Public class Rectangle : Calculation1
    {

        // contractor to asign class variables
       Public Rectangle(Double length, Double width)
        {
            this.length = length;
            this.width = width;
        }

        // overriding the base class function
        Public override Double getArea()
        {
            Double area =this.length * this.width;
            Return area;
        }

        // calling base class methods
        Public Double getCircum()
        {
            Double circum = this.geRectangle_Circumference(this.length , this.width);
            Return circum;
        }

    }
}

 Output















Note:-

In above example I created the Constructor in both derived class to asign the variables of Abstract base calss.

.





No comments:

Post a Comment