Abstraction is one of the most important concepts of object-oriented programming. From it comes from how objects are drawn in the form of classes, their characteristics are idealized as attributes and functionality as methods.
The concept is based on the following: from a real situation, the subjects involved, inanimate or not, are converted to the code in the form of objects. We can use the example of a lynx that needs to be registered in a zoo system.
We will work with the example mentioned above using the PHP language. First, then, let’s create the “Lynx” class.
class Linx{
}
Our object is now created. Below we will implement some attributes which are nothing more than the subject’s features.
class Linx{
private $name;
private $weight;
private $id_ident;
}
Attributes can also consider information as personal documents and unique identifiers. The variables in this case are declared as private so that treatment of them is done using another concept of object-oriented programming: encapsulation.
Created the object and its attributes, it is now necessary to create the features in the form of methods.
class Employee{
private $name;
private $weight;
private $id_ident;
function Register ()
{
//code here
}
function Delete()
{
//code here
}
}
With the created methods, it is now possible to register and delete any Lynx that arrives at the zoo. These would be the most basic methods. Thus, many others may be created in order to make the most complete object.
The real-world objects abstraction capacity for programming is essential for the development of activities of any programmer. Understanding the concept and applying it properly will save hours of development and ensure greater productivity to the environment.
Check out more content on our blog!
Learn all about Scriptcase.
You might also like…