What is “A PIE” ? “A PIE” is an abbreviation for “Abstraction, Polymorphism, Inheritance, Encapsulation”, four pillars of object-oriented programming. These concepts plays a crucial role in creating a robust, maintainable, and flexible codebase. Let’s delve deeper into each of these concepts.
Abstraction
Abstraction is hiding information that you don’t need to know to use a method or a class. It means that a lot of details of a class or an object are stripped away to reduce something to its core essence.
Polymorphism
Polymorphism is a way of implementing abstract functionality. It is an ability to react to different objects, yet share the same methods.
Inheritance
Inheritance is a mechanism to represent groups of similar types. It allows a class to inherit properties and behaviors from another class.
Encapsulation
Encapsulation is wrapping code and data into a single unit, limiting access to some of that data. It prevents external code from being concerned with internal workings of an object.
Example
A company’s positions are divided into two roles: manager, and engineer. I will develop a system to retrieve data about each employee.
First, I will create an Employee
class to represent these roles.
|
|
|
|
|
|
Engineer
and Manager
classes are an example of inheritance, as they share similar attributes: first name, last name, and salary. They extend Employee
class to inherit this data instead of recreating the same attributes.
Additionally, getData
illustrates abstraction. We don’t need to print the information by getting the value of first name, last name, and salary data; calling the getData
method is enough.
The existing implementation is not secure because anyone can modify the data. This happens because the properties are not properly protected. Let’s enhance the security by adding access modifiers.
|
|
|
|
|
|
By adding access modifiers, we are practicing encapsulation concept because the access to data is limited and exposed through getter functions.
Let’s create a test class
|
|
Output:
[Engineer] First Name: Foo
Last Name: Bar
Salary: 100
[MANAGER] First Name: Foo2
Last Name: Bar2
Salary: 250
This is an implementation of polymorphism where the Employee
class responds differently to the getData
function when it is used by the Engineer
and Manager
classes.