Learn OOPS in the right way

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.

1. Abstraction

Source Crio.Do
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

Source Crio.Do

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

Source Crio.Do

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

  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.

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.

  • The functions should have the same name
  • Parameters should be same in number and type
  • The classes should be different for the overriding functions
  • 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.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Vishnu Kalyan

Vishnu Kalyan

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