C# Abstraction

  • Abstraction is a process in which only the functionality is shown and it’s definition remains hidden. Abstraction can be achieved by Abstract classes.
  • An abstract class does not contain any definition and is implemented in another outside class.
  • An abstract method or class has abstract keyword before it’s return type.
// An abstract method.
public abstract void vol();
// An abstract class.
asbtract class AbstractExample
  • If a class is made abstract then it’s methods can also  be of abstract type and at least one method will be of abstract type.
Inheritance
  • Another way to achieve abstraction is by creating interfaces. An Interface only contains the declaration of methods data members and does not have any implementation of it.
  • An Interface must be inherited by another class to make the implementations of interface’s methods.
using System;

interface Fruits{
    void enter();
    void show();
}
//Interface is inherited by another class and implement.
class Basket: Fruits{
    int no;
    public void enter()
    {
        Console.WriteLine("Enter the number of fruits available: ");
        no = Console.ReadLine();
    }
    public void show()
    {
        Console.WriteLine("Available number od fruits are: ", no);
    }
}