What is Abstraction in C# with example?


Abstract Class:-

Abstract classes are the way to achieve abstraction in C#. Abstraction in C# is the process to hide the internal details and showing functionality only. Abstraction can be achieved by two ways:


     A-Abstract class

     B-Interface


Abstract class and interface both can have abstract methods which are necessary for abstraction.

Abstract Method:-

A method which is declared abstract and has no body is called abstract method. It can be declared inside the abstract class only. Its implementation must be provided by derived classes. 

For example: public abstract void draw(); 

Impression : An abstract method in C# is internally a virtual method so it can be overridden by the derived class.

Abstract class:-

In C#, abstract class is a class which is declared abstract. It can have abstract and non-abstract methods. It cannot be instantiated. Its implementation must be provided by derived classes. Here, derived class is forced to provide the implementation of all the abstract methods.



Example

 class Program
    {
        static void Main(string[] args)
        {

            Square ob = new Square(4);
            double result = ob.Area();
            Console.WriteLine(result);
            Console.ReadLine();
        }
    }
    abstract class Shape
    {
        public abstract int Area();
    }
    class Square : Shape
    {
        private int Side;
        public Square(int x = 0)
        {
            Side = x;
        }
        public override int Area()
        {
            Console.Write("Area of Square: ");
            return (Side * Side);
        }
    }
Share:

No comments:

Post a Comment

Friday, 28 June 2019

What is Abstraction in C# with example?


Abstract Class:-

Abstract classes are the way to achieve abstraction in C#. Abstraction in C# is the process to hide the internal details and showing functionality only. Abstraction can be achieved by two ways:


     A-Abstract class

     B-Interface


Abstract class and interface both can have abstract methods which are necessary for abstraction.

Abstract Method:-

A method which is declared abstract and has no body is called abstract method. It can be declared inside the abstract class only. Its implementation must be provided by derived classes. 

For example: public abstract void draw(); 

Impression : An abstract method in C# is internally a virtual method so it can be overridden by the derived class.

Abstract class:-

In C#, abstract class is a class which is declared abstract. It can have abstract and non-abstract methods. It cannot be instantiated. Its implementation must be provided by derived classes. Here, derived class is forced to provide the implementation of all the abstract methods.



Example

 class Program
    {
        static void Main(string[] args)
        {

            Square ob = new Square(4);
            double result = ob.Area();
            Console.WriteLine(result);
            Console.ReadLine();
        }
    }
    abstract class Shape
    {
        public abstract int Area();
    }
    class Square : Shape
    {
        private int Side;
        public Square(int x = 0)
        {
            Side = x;
        }
        public override int Area()
        {
            Console.Write("Area of Square: ");
            return (Side * Side);
        }
    }

No comments:

Post a Comment

Popular

Total Pageviews

Archive