Notice that all “AdminEmployee” is an Employee and all “ServiceEmployee” is an Employee. So, how can we model this situation using OOP? Abstract class Employee in Javaįrom the description above, we can identify three classes: Employee, “AdminEmployee” and “ServiceEmployee”. Notice that this is a simplified example. For the service employees, we calculate the salary as basic salary + 6% of the basic salary. If position is 1 then the salary is 5000 and if it is 2 the salary is 8000. Admin employees have a salary that is calculated according to the level of their position. Let’s say we have two types of employees in a certain company: admin and service employees. It is also one of the most common use cases when teaching/learning inheritance.Īs you should remember, OOP is about modelling situations from real life. In this case, the method salary must be abstract in the class employee and have the respective implementation in the subclasses according to the type of employee.įind below a more detailed example related to employees’ salaries. This is a typical case of why we need abstract classes. Notice that although an employee has a salary, we need to know the type of employee (part-time or full-time) to calculate it. In this way, both types of employees will inherit this method. Therefore, we should have a method to calculate a salary in our base class Employee. Salary is something that should be calculated for every employee. However, they still have some attributes and functionalities in common: name, surname, id, salary, etc. But we must model two types of employees: part-time and full-time employees.Īs you should understand, the company calculates the salary for these two types of employees in a different way. Remember that before starting coding, we should first design what we want to code.Īn important part of our design is to identify classes and the relationships among them (compositions, aggregations, use, and inheritance).Īt this time, we can identify two (or more) classes that inherit from a common class.Īs an example, let’s imagine we are modelling a situation where we must calculate employee salaries. If you are not sure of what inheritance is, you should first read this post so you can understand what an abstract class is and why we need them in OOP. Why do we use an abstract classĪbstract classes are closely related to the concept of inheritance in OOP. Let’s find out what are possible scenarios where we can use abstract classes. Ok, but what can I do with this? When should I use an abstract class? In other words, you cannot create an object using the abstract class. It gives us the possibility of modelling concepts from the real world and facilitates the use of one of the OOP principles: code reuse.Īn abstract class in Object-Oriented Programming (OOP) is a class that cannot be instantiated. Abstract class concept is one of the basic concepts of Object-Oriented Programming.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |