It is a very common discussion when to use
Abstract
class and what is the benefit of using Abstract
class in real time application development. The common properties of Abstract
class are it cannot be initiated, functions and implementation can be partially implemented.
Consider an application that calculates salary of full time and contract based employees. There are few common properties of both employees, e.g., both employees have name, address, ID, but different way of calculating salaries. So we can declare one master class named as
BaseEmployee
and place common properties and Virtual Salary Calculator function with no implementation. Two child classes, FullTimeEmployee
and ContractEmployee
can inherit from BaseEmployee
class and override Salary Calculator virtual
function and our problem can be solved, then why Abstract
Class should be used?