4 OPP Essentials every programmer should know
Before diving deep into the fancy big technical words like “Software Engineering” and “Data Science”, we programmers forget to focus on the basic and essential topics of programming. Let’s understand and unlock the basics first.
1. Encapsulation
Encapsulation restricts the direct access to data members of a class due to which external objects cannot access them directly. We can achieve encapsulation by changing the object’s state restricted to private access.
Encapsulation can be implemented when we have to design classes which have data fields and need methods to be supported for their reading and modification. In the given class, public method for reading and/or modifying the data fields can ensure encapsulation.
Encapsulation comes with many advantages;
- Reduces / removes duplicate code
- Leads to high maintainability
- Ensures consistent behavior
- Ease in testing
What’s interesting is python — the so called easy language — doesn’t support this behavior by default. The only variables that starts with __ are hidden. It is also interesting to note that the fields are set ti private if the programming language support encapsulation.
2. Abstraction
It is the concept of exposing only the required onto the information to the user and hiding the rest. Each abstract class consists of some exposed methods and some hidden methods and data fields. As a matter of fact, JVM and APIs can also be called abstraction since it hides or abstracts out the internal complexity.
Abstraction separates the method signature from its implementation. We also know that in a closely related class, we can separate the common fields/methods into a different class and reuse the same. It gives the implementer flexibility to change the method at later stage while the user does not have to bother with the internal complexity of the class. We can achieve abstraction by using a smaller set of public methods which is going to lead to high maintainability and reduced complexity.
In Java; interface
is used for classes that provide total abstraction and abstract
is used for classes that provides partial abstraction. In C++; we can use header files and provide methods to hide implementation details.
3. Inheritance
It is the concept of using the functionalities offered by another class and building on top of the same. Multiple new classes can build upon the same class. Inheritance as a whole, increases re-usability of code.
A child can inherit from parent class and reuse the common fields and methods leading to clean class structure. Inheritance comes into practice when different objects needs to share functionalities, thus avoiding redundancy in code.
There are many different types of inheritance. The name of the which are self explanatory;
- Single Inheritance
- Multiple Inheritance
- Hierarchical Inheritance
- Multilevel Inheritance
- Hybrid Inheritance
The general rule of Inheritance states that a child class can extend a parent class by inheriting its features which implements the DRY
(Don’t Repeat Yourself) programming principle.
4. Polymorphism
It is the mechanism which allows us to modify a functionality offered by a method depending on the situation. It gives us the ability to perform different activities using the same method signature (name) leading to cleaner code. Polymorphism comes into action when the child classes needs to implement a different logic or needs different parameters than used by the parent class. It can be implemented by overriding or overloading methods in the parent class.
Method Override
When the child class implements a method which is already inherited from the parent class, it results in the a conflict. The conflict is resolved by invoking the child class method. This mechanism is also known as Dynamic Polymorphism.
Method override is always build on top of inheritance. Same name functionality, same number and type of parameters in both functionality, and different class are necessary requirements for performing method override. The most important advantage of the same is that the program clients are not impacted by the same.
Method Overload
A conflict arises while writing multiple methods with the same name but different sets/ order of parameters which is resolved by invoking appropriate method on the basis of the passed parameters. This mechanism is also popularly known as Static Polymorphism.
Unlike override, method overload can be implemented with any class. In longer code-base, modification of a method by overloading in one place is popularly preferred. The necessary requirements of method overload includes same functionality name, different number or types of parameters and different classes. Even though the mechanism promises to cleaner code, too many overload methods can make the code hard to follow.
Encapsulation. Abstraction. Inheritance. Polymorphism.
These are the 4 basic concepts one should understand before diving deep into programming. Be it SDE interviews or just programming in general, these are the fundamental concepts of object oriented programming and an essential for every programmer.
Follow my journey as a computer science enthusiast on instagram: shecodes._