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:
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()); }
Avery good write-up. Please let me know what are the types of C# libraries used for AI development.
very satisfied!!
Good tutorial. Small question: Say, there is : enum numbers { one, two, three} and a string field_enum ="one" how would I from the variable field_enum have a response with value numbers.one so that it can be treated as an enum and not as a string. making a list from the enum, and loop into the list. is not elegant... and may not work is forced value on field is forced ( one = 100).
Hi Team Knowledge Hut, Thank you for such an informative post like this. I am completely new to this digital marketing field and do not have much idea about this, but your post has become a supportive pillar for me. After reading the blog I would expect to read more about the topic. I wish to get connected with you always to have updates on these sorts of ideas. Regards, Kshitiz
The reason abstraction can be used with this example is because, the triangle, circle. Square etc can be defined as a shape, for example.....shape c = new circle(5,0)...the abstract object c now points at the circle class. Thus hiding implementation
Leave a Reply
Your email address will not be published. Required fields are marked *