top

Search

C# Tutorial

Abstraction is an important part of object oriented programming. It means that only the required information is visible to the user and the rest of the information is hidden.Abstraction can be implemented using abstract classes in C#. Abstract classes are base classes with partial implementation. These classes contain abstract methods that are inherited by other classes that provide more functionality.Some of the salient points about abstract classes are as follows:The abstract class is created using the keyword abstract and some of the methods of the abstract class also contain the keyword abstract.No object can be created of the abstract class i.e.it cannot be instantiated.The abstract methods in the abstract class are implemented actually only in the derived classes.If all the methods in the abstract class contain the keyword abstract, then that class is known as pure Abstract class.A program that demonstrates abstraction is given as follows:Source Code: Program that demonstrates abstraction in C#using System; namespace AbstractionDemo {    abstract class Shape    {      public abstract double area();    }    class Circle: Shape    {      private double radius;      public Circle( double r)      {         radius = r;      }      public override double area ()      {         return (3.14*radius*radius);      }    }    class Square: Shape    {      private double side;      public Square( double s)      {         side = s;      }      public override double area ()      {         return (side*side);      }    }     class Triangle: Shape    {      private double tbase;      private double theight;      public Triangle( double b, double h)      {         tbase = b;         theight = h;      }      public override double area ()      {         return (0.5*tbase*theight);      }   }    class Test    {      static void Main(string[] args)      {         Circle c = new Circle(5.0);         Console.WriteLine("Area of Circle = {0}", c.area());         Square s = new Square(2.5);         Console.WriteLine("Area of Square = {0}", s.area());         Triangle t = new Triangle(2.0, 5.0);         Console.WriteLine("Area of Triangle = {0}", t.area());      }    } }The output of the above program is as follows:Area of Circle = 78.5 Area of Square = 6.25 Area of Triangle = 5Now let us understand the above program.The abstract class shape contains the abstract method area(). The code snippet for this is given as follows:abstract class Shape    {      public abstract double area();    }The class Circle inherits from Shape. It has a private data member radius. The parameterized constructor assigns a value to radius. The function area() calculates the area of the circle and returns that value. The code snippet for this is given as follows:class Circle: Shape    {      private double radius;      public Circle( double r)      {         radius = r;      }      public override double area ()      {         return (3.14*radius*radius);      }    }The class Square inherits from Shape. It has a private data member side. The parameterized constructor assigns a value to side. The function area() calculates the area of the square and returns that value. The code snippet for this is given as follows:class Circle: Shape    {      private double radius;      public Circle( double r)      {         radius = r;      }      public override double area ()      {         return (3.14*radius*radius);      }    }The class Square inherits from Shape. It has a private data member side. The parameterized constructor assigns a value to side. The function area() calculates the area of the square and returns that value. The code snippet for this is given as follows: class Square: Shape    {      private double side;      public Square( double s)      {         side = s;      }      public override double area ()      {         return (side*side);      }    }The class Triangle inherits from Shape. It has private data members tbase and theight. The parameterized constructor assigns a value to tbase and theight. The function area() calculates the area of the triangle and returns that value. The code snippet for this is given as follows:class Triangle: Shape    {      private double tbase;      private double theight;      public Triangle( double b, double h)      {         tbase = b;         theight = h;      }      public override double area ()      {         return (0.5*tbase*theight);      }     }The function main() contains the objects c, s and t for classes Circle, Square and Triangle respectively. Then the areas of the circle, square and triangle are printed using the function area(). The code snippet for this is given as follows: static void Main(string[] args)      {         Circle c = new Circle(5.0);         Console.WriteLine("Area of Circle = {0}", c.area());         Square s = new Square(2.5);         Console.WriteLine("Area of Square = {0}", s.area());         Triangle t = new Triangle(2.0, 5.0);         Console.WriteLine("Area of Triangle = {0}", t.area());      }
logo

C# Tutorial

Abstraction in C#

Abstraction is an important part of object oriented programming. It means that only the required information is visible to the user and the rest of the information is hidden.

Abstraction can be implemented using abstract classes in C#. Abstract classes are base classes with partial implementation. These classes contain abstract methods that are inherited by other classes that provide more functionality.

Some of the salient points about abstract classes are as follows:

  1. The abstract class is created using the keyword abstract and some of the methods of the abstract class also contain the keyword abstract.
  2. No object can be created of the abstract class i.e.it cannot be instantiated.
  3. The abstract methods in the abstract class are implemented actually only in the derived classes.
  4. If all the methods in the abstract class contain the keyword abstract, then that class is known as pure Abstract class.

A program that demonstrates abstraction is given as follows:

Source Code: Program that demonstrates abstraction in C#

using System;
namespace AbstractionDemo
{
   abstract class Shape
   {
     public abstract double area();
   }
   class Circle: Shape
   {
     private double radius;
     public Circle( double r)
     {
        radius = r;
     }
     public override double area ()
     {
        return (3.14*radius*radius);
     }
   }
   class Square: Shape
   {
     private double side;
     public Square( double s)
     {
        side = s;
     }
     public override double area ()
     {
        return (side*side);
     }
   }
    class Triangle: Shape
   {
     private double tbase;
     private double theight;
     public Triangle( double b, double h)
     {
        tbase = b;
        theight = h;
     }
     public override double area ()
     {
        return (0.5*tbase*theight);
     }
  }
   class Test
   {
     static void Main(string[] args)
     {
        Circle c = new Circle(5.0);
        Console.WriteLine("Area of Circle = {0}", c.area());
        Square s = new Square(2.5);
        Console.WriteLine("Area of Square = {0}", s.area());
        Triangle t = new Triangle(2.0, 5.0);
        Console.WriteLine("Area of Triangle = {0}", t.area());
     }
   }
}

The output of the above program is as follows:

Area of Circle = 78.5
Area of Square = 6.25
Area of Triangle = 5

Now let us understand the above program.

The abstract class shape contains the abstract method area(). The code snippet for this is given as follows:

abstract class Shape
   {
     public abstract double area();
   }

The class Circle inherits from Shape. It has a private data member radius. The parameterized constructor assigns a value to radius. The function area() calculates the area of the circle and returns that value. The code snippet for this is given as follows:

class Circle: Shape
   {
     private double radius;
     public Circle( double r)
     {
        radius = r;
     }
     public override double area ()
     {
        return (3.14*radius*radius);
     }
   }

The class Square inherits from Shape. It has a private data member side. The parameterized constructor assigns a value to side. The function area() calculates the area of the square and returns that value. The code snippet for this is given as follows:

class Circle: Shape
   {
     private double radius;
     public Circle( double r)
     {
        radius = r;
     }
     public override double area ()
     {
        return (3.14*radius*radius);
     }
   }

The class Square inherits from Shape. It has a private data member side. The parameterized constructor assigns a value to side. The function area() calculates the area of the square and returns that value. The code snippet for this is given as follows:

 class Square: Shape
   {
     private double side;
     public Square( double s)
     {
        side = s;
     }
     public override double area ()
     {
        return (side*side);
     }
   }

The class Triangle inherits from Shape. It has private data members tbase and theight. The parameterized constructor assigns a value to tbase and theight. The function area() calculates the area of the triangle and returns that value. The code snippet for this is given as follows:

class Triangle: Shape
   {
     private double tbase;
     private double theight;
     public Triangle( double b, double h)
     {
        tbase = b;
        theight = h;
     }
     public override double area ()
     {
        return (0.5*tbase*theight);
     }
    }

The function main() contains the objects c, s and t for classes Circle, Square and Triangle respectively. Then the areas of the circle, square and triangle are printed using the function area(). The code snippet for this is given as follows:

 static void Main(string[] args)
     {
        Circle c = new Circle(5.0);
        Console.WriteLine("Area of Circle = {0}", c.area());
        Square s = new Square(2.5);
        Console.WriteLine("Area of Square = {0}", s.area());
        Triangle t = new Triangle(2.0, 5.0);
        Console.WriteLine("Area of Triangle = {0}", t.area());
     }

Leave a Reply

Your email address will not be published. Required fields are marked *

Suggested Tutorials

Swift Tutorial

Introduction to Swift Tutorial
Swift Tutorial

Introduction to Swift Tutorial

Read More

R Programming Tutorial

R Programming

Python Tutorial

Python Tutorial