Learn OOPS in the right way

Vishnu Kalyan
7 min readAug 1, 2020

#IBelieveInDoing Challenge July Edition

Crio.Do is a platform built to Build real products to learn tech hands-on with multiple micro experiences to work with. Crio helps in getting work-like software product development experience and learn the tech skills you need to excel in your career.

Crio follows #LearnByDoing methodology which motivates every developer to learn by actually doing it.

Introduction

I recently participated in the #IBelieveinDoing July edition challenge organised by Crio.do where all the participants will get a chance to work on (so called) bytes covering all the OOPs essentials.

So Lets see what is OOPs?

Object-Oriented Programming or OOPs refers to languages that uses objects in programming. The main aim of OOP is to bind together the data and the functions that operate on them so that no other part of the code can access this data except that function.

Now lets dive into the different characteristics of Object Oriented Programming….

1. Abstraction

Abstraction is exposing only the functions to the user rather than exposing the implementation details of it.

A well known online shopping site like Flipkart or Amazon has many products (or micro-services) that work behind the scenes. Some of these are user session management, inventory management, logistics, vendor management, search etc. These come together to take orders, orchestrate and finally deliver the order to the customer.

Source Crio.Do

Config Manager can leverage to read or update operations of every product’s configuration parameters. The Config Manager currently supports configuration in the form of a JSON file.

Some teams want to move towards XML file based configuration instead of JSON, since XML supports a good set of tools to build, encode, validate and build schemas.

One way to implement XML support is to add new methods to read & write XML configuration files inside our ConfigManager class. The Logistics team has shifted to XML based configuration, while the Inventory and Vendor teams stay with JSON.

Non Object Oriented Implementation

Why should we think about Abstraction here?

Abstraction addresses these shortcomings by clearly defining a common method signature. Separate classes can implement the same method but internally read the file type of choice. From a user perspective, the methods don’t change based on file type and it is easy to change from one type to another with minimal changes.

Redesign using Abstraction

What is the drawback if we don’t use abstraction?

Low maintainability because responsibilities are not clearly differentiated. Higher Complexity with larger code bases because many objects interact with others and it becomes difficult to add functionality without impacting others.

2. Encapsulation

Encapsulation is achieved by the object making its state (data fields) restricted to private access. External objects cannot access them directly. They can only access the state through the methods exposed by the objects.

Source Crio.Do

A well-known online shopping site offers the users multiple ways to access it. The users can use either the Web Browser, an Android App or an iOS App to access it. The User Preference object is one such common structure which stores the user’s preferences. Currently, User Preferences include two fields country and preferred language.

With the existing user preferences, some users end up changing the language to one that they don’t know (by mistake or maybe to fool around!). Then, they struggle to change it back resulting in a few customer complaints.

To address this, a new feature has been requested by the customer service team to restrict the languages based on the country chosen.

Encapsulation is achieved by the object making its state (data fields) restricted to private access. External objects cannot access them directly. They can only access the state through the methods exposed by the objects.

What is the drawback if we don’t use encapsulation?

Duplication of code. Low maintainability. Inconsistent behavior. Difficult to test. Possible inappropriate access/modification from external methods.

3. Inheritence

Inheritance is the process where one class acquires the properties (methods and fields) of another.

Source Crio.Do

A well known company was one of the first to support Text Messaging through an App. However, in recent years, with the advent of more advanced Messaging apps like Whatsapp, Viber etc., it isn’t able to attract new users.

In order to start enhancing the App Voice message and Image messagemust be supported. This is the situation to bring inheritance.

What does the Inheritance based design look like?

For each of the message types, namely, Text, Image & Voice, we create new children classes TextMessage, ImageMessage & VoiceMessage. These children classes inherit the Message class and hence need only add any fields and methods specific to them.

What is the drawback if we don’t use inheritance?

Code duplication, Higher maintenance cost.

Encapsulation and Abstraction vs Inheritance

  • Encapsulation and Abstraction are helpful to develop and maintain a big codebase.
  • When there are similar objects in this big codebase that share common functionality, the common functionality and fields can be separated out into a separate base class which is then inherited by child classes.

4. Polymorphism

Polymorphism is the ability of an object to take on many forms. The most common use of polymorphism in OOP occurs when a parent class reference is used to refer to a child class object.

The company wants to continue improving the Messaging Application. Each Message Type is now handled by different teams. The teams want more flexibility.

They don’t want to use the functionality provided by some of the methods in the base class. The teams want to have a way where some clients of this class can initialize the default values for some of these variables to a value of their choice.

Polymorphism can be achieved in two ways. The Messaging Application now has an Inheritance based design to support multiple Message Types.

  1. Method Overloading (Static) — Writing multiple methods with the same name but with different sets of parameters (or order of parameters) so that the appropriate method gets invoked based on the parameters passed.
  2. Method Overriding (Dynamic) — Child class implements a method that is already present in the parent class, resulting in the child method being invoked.

Method Overloading:

In method overloading in C++ or Java, the original method as well as the new overloaded method can be invoked by passing the different input parameters needed by each method.

Here, we have overloaded the__init__() methods to pass 2 new parameters. If those parameters are not passed, they will be initialized to None as seen in the parameter list passed to the__init__() method.

Method Overriding:

Child classes can choose to use either the default method in the base class or “override” the original implementation in the base class with their own logic.

As any child class of Message will be overriding the is_valid() method to implement their own logic for the message’s validity, clients of these classes need not worry. Even with any new message types, the child class would inherit from the base class and implement this method. So, no change/addition would be required on the client side.

What is the drawback if we don’t use polymorphism?

Maintaining code will be harder as some of the changes would require corresponding changes in all of the dependent/client code.

Summary of Polymorphism

Method overriding builds on top of inheritance and it can be done within any class. It presents the ability to perform different actions using the same method signature, keeping code cleaner.

Rules for overriding are

  • The functions should have the same name
  • Parameters should be same in number and type
  • The classes should be different for the overriding functions

Rules for overloading are

  • Function names should be the same
  • Parameters should be different in numbers or in types
  • Functions should belong to the same class

Conclusion

Now, with this knowledge about the OOPs concepts I can answer questions related to OOPs in more technical way than before. Thank You Crio.Do for this wonderful experience. I would love to participate in more such challenges.

--

--

Vishnu Kalyan

Tech-Enthusiast | 2x Microsoft Certified | One-side lover of Google