What is Abstraction in Object Oriented Programming – OOPs

Abstraction is the process of hiding certain or common details and showing only essential information to the user.
Abstraction can be achieved with either abstract classes or interfaces.

Abstraction by an Abstract Classes

Abstract Classes
— A restricted class that cannot be instantiated directly i.e. cannot be used to create objects (to access it, it must by inherited
from another class).
— An abstract class can have both abstract and non-abstract members and and accessors in it.
— An abstract class is declared with the help of abstract keyword.
— Abstract classes may not be marked as private or final or Sealed.
— An abstract class that extends another abstract class inherits all of its abstract methods as its own abstract methods.
— The first Concrete Derived class that extends an abstract class must provide an implementation for all of the inherited abstract
methods.

Abstract Method
— Abstract methods have no implementation, its method definition is followed by a semicolon instead of a normal method block.
— An abstract method is implicitly a virtual method.
— Abstract method declarations are only permitted in abstract classes.

abstract class Employee
{
    public int ID { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }

 

    public string GetFullName()
    {
        return this.FirstName + ” ” + LastName;
    }
    public abstract int GetMonthlySalary(); //abstract method
}

 

class HourlyEmployee : Employee
{
    public HourlyEmployee(int HourlyPay, int TotalHours)
    {
        this._HourlyPay = HourlyPay;
        this._TotalHours = HourlyPay;
    }
    protected int _HourlyPay { get; set; }
    protected int _TotalHours { get; set; }

 

    public override int GetMonthlySalary()
    {
        return this._TotalHours * this._HourlyPay;
    }
}

 

class FullTimeEmployee : Employee
{
    public FullTimeEmployee(int AnnualSalary)
    {
        this._AnnualSalary = AnnualSalary;
    }
    protected int _AnnualSalary { get; set; }
    public override int GetMonthlySalary()
    {
        return this._AnnualSalary / 12;
    }
}

 

public class ManageEmployee
{
    public void createEmployees()
    {
        FullTimeEmployee fullTime = new FullTimeEmployee(1000000);
        fullTime.GetFullName();
        fullTime.GetMonthlySalary();

 

        HourlyEmployee _hourly = new HourlyEmployee(500, 50);
        _hourly.FirstName = “First”;
        _hourly.LastName = “Last”;
        _hourly.GetMonthlySalary();
    }
}
Abstract Class and Inheritance
When an abstract class inherits a virtual method from a base class, the abstract class can override the virtual method with an abstract method.
abstract class Employee
{
    public virtual int GetMonthlySalary(int hours)
    {
        // Original implementation.
        return 50000;
    }
}
class HourlyEmployee : Employee
{
    public abstract override int GetMonthlySalary(int hours);
}
class Contractors : HourlyEmployee
{
    public override int GetMonthlySalary(int hours)
    {
        return hours * 350;
    }
}
Abstraction by an Interface
Interface
A completely “abstract class”
An interface is similar to an abstract class but all of its methods are abstract by default. It means that it can’t have non-abstract
methods.
— Like abstract classes, interfaces cannot be used to create objects.
— On implementation of an interface, you must override all of its methods
— Interfaces can contain properties and methods, but not fields/variables
— An interface cannot contain a constructor
— Interface can have methods, properties, events, and indexers as its members. But interfaces will contain only the declaration of
the members. The implementation of interface’s members will be given by the class who implements the interface implicitly or
explicitly.
— Interface methods are abstract and public by default
interface IEmployee
{
    string Name { get; set; }
    DateTime DateofBirth { get; set; }
    ushort Age();
}
interface IContact
{
    string Street { get; set; }
    uint Zip { get; set; }
    string State();
}
public class CoreStaff : IEmployee, IContact
{
    //—IPerson—
    public string Name { get; set; }
    public DateTime DateofBirth { get; set; }
    public ushort Age()
    {
        return (ushort)(DateTime.Now.Year – this.DateofBirth.Year);
    }
    //—IAddress—
    public string Street { get; set; }
    public uint Zip { get; set; }
    public string State()
    {           
        return “Delhi”;
    }
}

Abstract class Vs Interface
— An Abstract Class can contains both declaration and definition part where as an Interface can contains only a declaration part.
— Multiple inheritance is not possible by an abstract class where an interface can do Multiple inheritance.
— An Abstract Class can contain a constructor where as an interface do have any constructor.
— An Abstract Class can contain static members, which is not feasible inside an interface.
— An Abstract Class can contain different types of access modifiers like public, private, protected etc where as interface members
are public by default.
— An Abstract Class is used to implement the core identity of class where as an interface is used to implement peripheral abilities
of class.
— A concrete class can only use/implement/inherit one abstract class at a time, where as a concrete class can use/implement/inherit
multiple interface.
— An Abstract Class can be fully or partially or even not implemented after inherit, where as an interface should be fully implemented
once inherited.

When to Use?
If many implementations or sub classes are of the same kind and use common behavior, then it is superior to use an abstract class.
If many implementations or sub classes only share methods, then it is superior to use Interface.

When base class should provide default implementation of certain methods whereas other methods should be open to being
overridden by child classes use abstract classes.
When all child classes are compulsory to all implement a certain group of methods/functionalities and each of the child classes is
free to provide its own implementation then use interfaces.

Leave a Reply

Your email address will not be published.