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.
{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();}}
{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;}}
{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.
India showcased dominance, defeating West Indies by 211 runs in the first ODI of the… Read More
Discover the easy Hindi lyrics of Shree Kali Mata Ki Aarti and embrace the power… Read More
Discover the significance of Ganga Maiya Ki Aarti in Hindi and immerse yourself in this… Read More
Discover the significance of Khatu Shyam Ji Ki Aarti and its beautiful verses in Hindi… Read More
Discover Shree Ram Ji ki aarti in Hindi with easy lyrics for everyone. Immerse in… Read More
Read and learn Vishnu Ji Ki Aarti in Hindi to enrich your daily spiritual practice… Read More