OOP Design Principles
An application that implements Object-Oriented Programming concepts is distinguished by four design principles. At times, I used to think that understanding Object Oriented Programming concepts have made it difficult since they have grouped under four main concepts, while each concept is closely related with one another. Hence one has to be extremely careful to correctly understand each concept separately, while understanding the way each related with other concepts. These four principles are encapsulation, abstraction, inheritance, and polymorphism.
Easily remembered as A-PIE
Abstraction refers to the act of representing essential features without including the background details or explanations. It is the ability to generalize an object as a data type that has a specific set of characteristics and is able to perform a set of actions. Object-oriented languages provide abstraction via classes. Classes define the properties and methods of an object type.
For example, consider a television as an object. It has a manual explaining how to use the television. However, this manual does not show all the technical details of the television, thus giving only an abstraction to the user.
Polymorphisms is a generic term that means ‘many shapes’. In other words, it means the ability to request that the same operations be performed by a wide range of different types of things. For instance, if both Customer and Vendor objects have a name property, and we can write a routine that calls the Name property regardless of whether we’re using a Customer or Vendor object, then we have polymorphism.
A vehicle is a good example of polymorphism. A vehicle interface would only have those properties and methods that all vehicles have, a few of which might include paint color, number of doors, accelerator, and ignition. These properties and methods would apply to all types of vehicles including cars, trucks, and semi-trucks.
Polymorphism will not implement code behind the vehicle’s properties and methods. Instead, polymorphism is the implementation of an interface. If the car, truck, and semitruck all implement the same vehicle interface, then the client code for all three classes can be exactly the same.
In OOP the polymorphisms is achieved by using many different techniques named method overloading, operator overloading and method overriding.
Inheritance is the idea of a class, called a subclass, based on another class, called a base class. Classes are created in hierarchies, and inheritance allows the structure and methods in one class to be passed down the hierarchy. That means less programming is required when adding functions to complex systems. Additional code may then be added to create a more specialized version of the class. If a step is added at the bottom of a hierarchy, then only the processing and the data associated with that unique step needs to be added. Everything else about that step is inherited. The ability to reuse existing objects is considered a major advantage of object technology.
Let’s take the Vehicles as examples.
Bikes and motorcycles are the subclasses from the two-wheeler vehicle, which acts as the base class for them. But two-wheeler vehicle class is the subclass to the class Vehicle. Each lower level in the hierarchy takes the characteristics of the ones before and add more specialized functions for its own.
Encapsulation is known as “data hiding” concept because it makes an object look like a black box. Acting as concealing the functionality of a class so that the internal operations are hidden from the programmer, encapsulation ensures that the users of an object cannot change the internal state of the object in unexpected ways. With correct encapsulation, the developer does not need to understand how the class actually operates in order to communicate with it via its publicly available methods and properties; known as its public interface.
Encapsulation is essential to creating maintainable OOP. When the interaction with an object uses only the publicly available interface of methods and properties, the class of the object becomes a correctly isolated unit. This unit can then be replaced independently to fix bugs, to change internal behavior or to improve functionality or performance.
There are several ways that an encapsulation can be used, as an example we can take the usage of an interface. The interface can be used to hide the information of an implemented class.
For example, in an ATM system, a client accesses his account by only inserting the card into the machine and giving it his password. Then he requests a withdrawal and if there is enough fund, he will get the money, if not his request will be rejected. The user doesn’t know the sequence of functions and operations that happen inside so that his request is fulfilled or not. The ATM is the encapsulated class.
Abstraction: the ability of a program to ignore the details of an object’s subclass and work at a more generic level when appropriate.
Polymorphism: the ability of objects belonging to diﬀerent types to respond to method calls to methods of the same name, each one according to the right type-speciﬁc behavior.
Inheritance: Objects can be deﬁned and created that are specialized types of already-existing objects.
Encapsulation: The class is kind of a container or capsule or a cell, which encapsulate the set of methods, attribute and properties to provide its indented functionalities to other classes.
This was briefly about the Object Oriented Programming concepts 😀
For any feedback or questions, you’re more than welcome to send me an email at email@example.com or reach me @yasabdelhady on twitter.