• Skip to main content
  • Skip to primary sidebar
BMA

BeMyAficionado

Inspire Affection

How to Start Thinking In OOPs (Object Oriented Programming) to Become the Best Programmer You Ever Wanted

July 29, 2016 by varunshrivastava 1 Comment

Table of Contents

  • Thinking in OOPS
    • What is OOPS?
    • Fundamental Concept Of OOP
      • Abstraction
      • Encapsulation
      • Inheritance
    • A Good Design
      • Single Responsibility Principle, in short, is S.R.P
      • Open Close Principle in short O.C.P,
      • Liskov Substitution Principle in short L.S.P
      • Interface Segregation Principle
      • Dependency Inversion Principle
    • Conclusion

Thinking in OOPS

Introduction to Object Oriented Programming

Object Oriented Programming is one of the most misunderstood concepts in today’s world.

It may seem difficult to accept the truth especially when every other programmer is using object oriented language to structure their code.

But do you really understand OOPS and the way it must be used to solve our problems better.

In this article, I have talked about the topic of Object Oriented Programming. What it actually is, its fundamental concept andhow to use it for a good design.

Stay with me for the next 5 minutes and it willhelp you to improve your understanding.

What is OOPS?

Do you know the full form of OOPS?

I mean OOP is very clear and it stands for Object Oriented Programming but what about ‘S’ in OOPS.

If you do not know then it’s fine because it is one of the most confused topics in OOPS.

Some say OOPS stands for Object Oriented Programming Structure, for some ‘S’ stands for System and they try to prove their point by saying that there is a famous conference named as Object Oriented Programming Conference and Structure and some say that ‘S’ doesn’t carry any meaning (bullshit).

I think it’s all nonsense to argue on the meaning of ‘S’.

I think OOP is more of a paradigm than a system.

A paradigm which enables us to think in a proper way, a paradigm that gives us the power to better understand real-world problems and model them through programming.

OOP is a paradigm which is organised around Objects rather than Actions, Data and logic.

Programming has evolved a lot in these past years.

If you compare:

Programming has been more of a logical procedure that takes input as data and performs the logical operation on it to obtain the desired result.

OOP enables you to create a modular design for your application.

I refer you below mentioned book,

Fundamental Concept Of OOP

Abstraction

Loose Coupling

Abstraction is one of the most fundamental concepts of oop.

It is also a very powerful concept. It helps to solve complex problems.

Abstraction is a concept of dealing with ideas rather than events It is a way to combine smaller things to make a bigger thing.

Suppose, you want to buy a car. You will go to the showroom and look for the complete car, though it is made of many different small modules that are responsible for their own specific task which when work in cohesion creates a single complex entity, which is in our case a CAR.

You will never get to know about the complexity beneath it. That is what abstraction is – Hiding the underlying complexity of the system from the User.

A process of taking away or removing characteristics from something in order to reduce it to a set of essential characteristics.

You will only worry about the looks, feel and superficial specification of the car. You will never ask the sales person to open up the engine to see how it works

Interfaces are a way to achieve Abstraction.

Encapsulation

Encapsulation is another fundamental concept of OOP.

This is again one of the most misunderstood concepts in “oop” because different people interpret it differently and that creates confusion.

You may know encapsulation as the hiding of data members and member functions which would simply mean hiding the internal representation of an object from the view outside of the Object’s definition.

Well, it’s not wrong to say that but it is a partial definition.

Look:

Encapsulation can be used to achieve data hiding but encapsulation itself is not data hiding.

In fact, In some OOP languages hiding of components is not automatic or can be overridden; thus, information hiding is defined by yet another notion.

There are two parts to encapsulation which is used to distinguish two distinct notions-

  1. A language mechanism for restricting direct access to some of the object‘s components.
  2. A language construct that facilitates the bundling of data with the methods (or other functions) operating on that data.

The feature of Encapsulation is often supported by the use of classes, although there are different ways to achieve that.

Inheritance

Inheritance in Object Oriented Programming

Third pillar or should I say mechanism to achieve OOP is Inheritance.

Every programmer is familiar with the concept of inheritance.

It provides code re-usability and power to create relationships between multiple objects

It is also used to extend the functionality of a class without changing the actual code of that class. This satisfies the Open/Closed principle of SOLID Design.

But often the concept of inheritance is misused.

Here’s the deal:

If you are using Inheritance only to import the functionality of a class into another then you are doing it wrong. By doing so, you are putting a lot of functionality on the base class.

This results in the creation of a GOD Object.

The real use of inheritance comes when you need to show a relation between any two entities (classic example would be Dog extends Animal). A parent class will not contain anything that would not make sense for its children.

For example – A bark() function would make sense for Dog but it will not make sense for a Fish.

You see, Inheritance is a very good feature of an OOP language. It gives us a way to model real world relations into code. Do not use it for wrong design or convenience.

Always prefer Composition over Inheritance if you need the functionality of the class. Only use inheritance if there is a real relation between the Parent and the Child.

A Good Design

Good Design

OOP is one of the most used concepts in today’s era.

I would not be 100% correct if I categorize it into a Right or Wrong Design. Because there is not right or wrong.

Simply put:

a good and a bad makes more sense.

Many developers and programmers who have been working in this field has tonnes of experience and has created their own theories and postulates which differentiate one design from another and motivates ones to use a certain design over the other.

Am I making sense?

Are you still with me?

Good.

What I’m trying to say here is that if you do not know what’s best, it is better to refer the design patterns which are known to have worked for many different organisations many times. Choose a design pattern that has been tested and the one that is working.

This will give you extra confidence in its implementation because it is already tested by large organisations.

You will also get to know about the pros and cons of using that particular design which will help you better analyse it.

If you are going to formulate your own design, then you must make sure you follow and obey the SOLID Design principles.

This will make your code clean, more independent, more maintainable and you will avoid smelly code.

These principles may sound tough in the beginning but are very easy to grasp.

  • S – Single-responsiblity principle
  • O – Open-closed principle
  • L – Liskov substitution principle
  • I – Interface segregation principle
  • D – Dependency Inversion Principle

 

Let’s take each principle one-by-one,

Single Responsibility Principle, in short, is S.R.P

It means:

A class should have one and only one reason to change, meaning that a class should have only one job.

Open Close Principle in short O.C.P,

It means:

Objects or entities should be open for extension, but closed for modification.

Liskov Substitution Principle in short L.S.P

It means:

Derived classes must be useful through the base class interface, without the need for the user to know the difference.

Interface Segregation Principle

The Interface Segregation Principle states that clients should not be forced to implement interfaces they don’t use. Instead of one fat interface, multiple small interfaces are preferred based on groups of methods, each one serving one sub-module.

In short:

Don’t be afraid to create multiple interfaces to sub-divide your module.

Dependency Inversion Principle

Check this document out: The Dependency Inversion Principle.

It basically says:

  1. High-level modules should not depend on low-level modules. Both should depend on abstractions.
  2. Abstractions should never depend on upon details. Details should depend on abstractions.

DIP reduces the coupling between two pieces of code.

This can be achieved only when you are more dependent on the concept rather than the implementation part of it.

Because you know that changes are risky.

It can really complicate or break your architecture in future.

Therefore, you must write your code in such a way which is time stable. Make your implementation depend on the interface. Dependency Inversion Principle can be achieved this way.

If your implementation depends on the interface then you get the power to choose which implementation is best at runtime and use that instead.

To provide a summary, the Dependency Inversion Principle is primarily about reversing the conventional direction of dependencies from “higher level” components to “lower level” components such that “lower level” components are dependent upon the interfaces owned by the “higher level” components. (Note: “higher level” component here refers to the component requiring external dependencies/services, not necessarily its conceptual position within a layered architecture).

In doing so, a coupling isn’t reduced as much as it is shifted from components that are theoretically less valuable for reuse to components which are theoretically more valuable for reuse.

Conclusion

OOP is one of the most widely used paradigms in today’s world.

The four pillars of OOP i.e, Abstraction, Encapsulation, Inheritance and Polymorphism enables us to write a better code in terms of data rather than logic.

If used properly while implementing SOLID Principles in your design pattern then you not only have created an awesome architecture but you are also saved from writing smelly code.

That’s how powerful OOP is once you conquer it.

I strongly suggest you to read SOLID Design Principles by Uncle Bob which will give you immense insights on the pros and cons of this paradigm and how to avoid the pitfalls.

Related

Filed Under: Programming Tagged With: design, inheritance, java, object, oops, programming, solid, solid principles

Primary Sidebar

Subscribe to Blog via Email

Do you enjoy the content? Feel free to leave your email with me to receive new content straight to your inbox. I'm an engineer, you can trust me :)

Join 874 other subscribers

Latest Podcasts

Recent Posts

  • Is The Cosmos a Vast Computation?
  • Building Semantic Search for E-commerce Using Product Embeddings and OpenSearch
  • Leader Election with ZooKeeper: Simplifying Distributed Systems Management
  • AWS Serverless Event Driven Data Ingestion from Multiple and Diverse Sources
  • A Step-by-Step Guide to Deploy a Static Website with CloudFront and S3 Using CDK Behind A Custom Domain

Recent Comments

  • Varun Shrivastava on Deploy Lambda Function and API Gateway With Terraform
  • Vaibhav Shrivastava on Deploy Lambda Function and API Gateway With Terraform
  • Varun Shrivastava on Should Girls Wear Short Clothes?
  • D on Should Girls Wear Short Clothes?
  • disqus_X5PikVsRAg on Basic Calculator Leetcode Problem Using Object-Oriented Programming In Java

Categories

  • Blogging
  • Cooking
  • Fashion
  • Finance & Money
  • Programming
  • Reviews
  • Software Quality Assurance
  • Technology
  • Travelling
  • Tutorials
  • Web Hosting
  • Wordpress N SEO

Archives

  • November 2024
  • September 2024
  • July 2024
  • April 2024
  • February 2024
  • November 2023
  • June 2023
  • May 2023
  • April 2023
  • August 2022
  • May 2022
  • April 2022
  • February 2022
  • January 2022
  • November 2021
  • September 2021
  • August 2021
  • June 2021
  • May 2021
  • April 2021
  • February 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • August 2020
  • July 2020
  • June 2020
  • May 2020
  • April 2020
  • February 2020
  • December 2019
  • November 2019
  • October 2019
  • August 2019
  • July 2019
  • June 2019
  • May 2019
  • April 2019
  • March 2019
  • January 2019
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • June 2018
  • May 2018
  • March 2018
  • February 2018
  • January 2018
  • December 2017
  • November 2017
  • October 2017
  • September 2017
  • August 2017
  • July 2017
  • June 2017
  • May 2017
  • April 2017
  • March 2017
  • February 2017
  • January 2017
  • December 2016
  • November 2016
  • October 2016
  • September 2016
  • August 2016
  • July 2016
  • June 2016
  • May 2016

Tags

Affordable Hosting (4) algorithms (4) amazon (3) aoc-2020 (7) believe in yourself (4) best (4) database (4) earn money blogging (5) education (4) elementary sorting algorithms (4) experience (3) fashion (4) finance (6) Financial Freedom (7) food (7) friends (3) goals (5) google (5) india (10) indian cuisine (5) indian education system (4) java (16) life (16) life changing (4) love (4) make money (3) microservices (9) motivation (4) oops (4) podcast (6) poor education system (4) principles of microservices (5) problem-solving (7) programmer (5) programming (28) python (5) reality (3) seo (6) spring (3) success (10) success factor (4) technology (4) top 5 (7) typescript (3) wordpress (7)

Copyright © 2025 · Be My Aficionado · WordPress · Log in

Go to mobile version