InterviewSolution
Saved Bookmarks
| 1. |
Abstraction in C# |
|
Answer» Abstraction means that only the information required is visible to the USER and the rest of the information is hidden from the user. This is an important part of object-oriented programming. Abstract classes are used to implement abstraction in C#. Abstract classes are base classes that have a partial implementation. These classes have abstract methods that are inherited by other classes that provide more functionality to the methods. A PROGRAM that demonstrates abstraction is given as follows: using System; namespace Demo { abstract class Shape { public abstract double AREA(); } class Rectangle: Shape { private double length; private double breath; public Rectangle( double l, double b) { length = l; breath = b; } public override double area () { return (length*breath); } } class Triangle: Shape { private double baseT; private double heightT; public Triangle( double b, double h) { baseT = b; heightT = h; } public override double area () { return (0.5*baseT*heightT); } } class Test { static VOID Main(string[] args) { Rectangle r = new Rectangle(8.0, 3.0); Console.WriteLine("Area of Rectangle = {0}", r.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 Rectangle = 24 Area of Triangle = 5 |
|