Object-Oriented Programming : comprehensive guide

Sameh Muhammed - Oct 8 '22 - - Dev Community

Motivation

It's very common to be asked about OOP concepts while you are still fresh graduate or junior developer, but what was surprising me why this question is still being asked even for mid level and maybe senior level positions, as these positions required a high level skills and asking about OOP is ridiculous, but what I noticed about this question is that, it can reflects your experience, it reflects how much fundamentals you absorbed of development skills, the quality of your words that describe OOP while you are moving from position to another should be different as a sign that you are really leveling up your skills, so let's talk about OOP in more comprehensive way.

Object-Oriented Programming

Object Oriented programming (OOP) is a programming paradigm that relies on the concept of classes and objects. It is used to structure a software program into simple, reusable pieces of code blueprints (usually called classes), which are used to create individual instances of objects.
OOP has four principles

  • Encapsulation
  • Inheritance
  • Polymorphism
  • Abstraction

Note: A lot of books and articles list OOP principles as just 3 (Encapsulation,Inheritance,Polymorphism), they considering Abstraction as part of Encapsulation principle, and this what we will follow here.

So, let's start describing each principal one by one.

Encapsulation

Encapsulation means that you grouping the related fields/data in one object so that you can treat them as one thing, but while you are grouping the fields you have to take in consideration some points

Cohesion

cohesion means that how much things are related to each other, so you have to make your fields inside one class highly cohesion.

for example, let's say that we have a Class that grouping Person data as below

Image description

You can notice that firstName, lastName and age are data that describes the main characteristics of person, so these fields are highly cohesion.

On the other hand, you will find that city, district and street, these fields are describe the person location, so they are high cohesion to each other, but related to other fields they are have low cohesion, so the best thing to do is to separate them into different class as below.

Image description

So that you are grouping the data that will be changed for same reason together.

Single Responsibility

Attaching multiple responsibilities for same class will end up with bloating the class with a lot of code that hard to track and change in the future, it's even better to have class that contains one field or one method than attach more responsibility to another class.

For example, adding calculateTax method to Person class as below, is violating Single Responsibility principle.

Image description

So, to be in safe side you should extract the calculateTax method into another class as below

Image description

Abstraction

Abstraction means hiding information from outside because outside is evil and can harm you, you have to expose only the minimal needed information to outside world, so using access modifiers properly is mandatory to protect your class, even public access modifier should be used wisely and consider using package access modifier whenever it possible

Image description

Inheritance

Inheritance is a way of reusing the code so that you can eliminate code duplication and make change easier.

Image description

But you should use inheritance wisely as it can harm your code, we talked in details about the dark side of inheritance here

https://www.linkedin.com/pulse/composition-inheritance-which-better-sameh-muhammed

Liskov Substitution Principle

LSP states that objects of a superclass should be replaceable with objects of its subclasses without breaking the application.

For example, as below code we have Bird class, Duck class which is subclass of Bird, but this type of inheritance is not correct as replacing the type in logic implemented on top of Bird class by Duck class may break the application.

Image description

Polymorphism

Polymorphism means different kinds of same thing, and it can be as

Method Overriding

means that same method with different logic, it's can be applied through inheritance as below

Image description

Method Overloading

Means that same method name with different signature, method signature includes method name, method parameter list and method return type in some programming language.

Image description

Subclassing

Inheritance or subclassing is a kind of polymorphism which can help you a lot in reusing code and generalization through following Open Closed Principle.

Open Closed Principle

This principle is getting benefit from subclassing and polymorphism, it enables you to write logic that can perform different behaviour based on different types as below.

Image description

When you are need to add more Calculators, all you need to create another class and extends TaxCalculator and provide your logic without affecting the pre-existing implementation.

Resources

IF YOU LIKED THE POST, THEN YOU CAN BUY ME A COFFEE, THANKS IN ADVANCE.

Buy Me A Coffee

. . . . . . . . . . . . . . . . . . . . . . . . . . . .