Technical note

4 OOP Essentials Every Programmer Should Know

Encapsulation, abstraction, inheritance, and polymorphism — the four pillars of object-oriented programming, explained from first principles.

Before diving deep into 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 direct access to the data members of a class, so external objects cannot access them directly. We achieve encapsulation by restricting an object's state to private access.

Encapsulation is useful when we design classes that have data fields and need methods to support reading and modifying them. In a given class, public methods for reading and/or modifying the data fields can ensure encapsulation.

Encapsulation comes with several advantages:

  • Reduces / removes duplicate code
  • Leads to high maintainability
  • Ensures consistent behavior
  • Eases testing

What's interesting is that Python — the so-called easy language — doesn't support this behavior by default. Only variables that start with __ are hidden. It's also worth noting that fields are set to private only if the programming language supports encapsulation.

2. Abstraction

Abstraction is the concept of exposing only the required information to the user and hiding the rest. Each abstract class consists of some exposed methods and some hidden methods and data fields. In fact, the JVM and APIs can also be called abstractions, since they hide or abstract away internal complexity.

Abstraction separates a method's signature from its implementation. In closely related classes, we can separate common fields and methods into a different class and reuse them. This gives the implementer flexibility to change a method later, while the user doesn't have to worry about the class's internal complexity. We achieve abstraction by using a smaller set of public methods, which leads to higher maintainability and reduced complexity.

In Java, an interface is used for classes that provide total abstraction, and abstract is used for classes that provide partial abstraction. In C++, we can use header files and provide methods to hide implementation details.

3. Inheritance

Inheritance is the concept of using the functionality offered by another class and building on top of it. Multiple new classes can build upon the same class. Inheritance, as a whole, increases the reusability of code.

A child class can inherit from a parent class and reuse its common fields and methods, leading to a clean class structure. Inheritance comes into practice when different objects need to share functionality, avoiding redundancy in code.

There are several different types of inheritance, and their names are largely self-explanatory:

  • Single Inheritance
  • Multiple Inheritance
  • Hierarchical Inheritance
  • Multilevel Inheritance
  • Hybrid Inheritance

The general rule of inheritance is 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

Polymorphism is the mechanism that allows us to modify the 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 play when child classes need to implement different logic or need different parameters than the parent class uses. It can be implemented by overriding or overloading methods in the parent class.

Method Override

When a child class implements a method already inherited from the parent class, it results in a conflict, which is resolved by invoking the child class's method. This mechanism is also known as dynamic polymorphism.

Method overriding is always built on top of inheritance. The same name, the same number and type of parameters in both methods, and different classes are necessary requirements for method overriding. Its most important advantage is that program clients aren't impacted by it.

Method Overload

A conflict arises when writing multiple methods with the same name but different sets or orders of parameters, which is resolved by invoking the appropriate method based on the parameters passed. This mechanism is also popularly known as static polymorphism.

Unlike overriding, method overloading can be implemented within any single class. In a larger codebase, modifying a method by overloading in one place is generally preferred. The requirements for method overloading are: the same functionality name, a different number or type of parameters, and it happens within the same class. Even though the mechanism promises cleaner code, too many overloaded methods can make the code hard to follow.

Encapsulation. Abstraction. Inheritance. Polymorphism.

These are the four basic concepts to understand before diving deep into programming. Be it SDE interviews or programming in general, these are the fundamental concepts of object-oriented programming, and essential for every programmer.

← All writing