Simple guide to Object Oriented Programming(OOP) and it’s Concepts

in learn •  6 years ago  (edited)

It is truly understandable that learning programming and writing to code can be totally frustrating and overwhelming. But, let me assure you that it's totally worth it. The right way to learn programming is to just jump into a project(you'll find plenty of step by step tutorial videos online) without thinking anything about it and complete the project even though you don't understand anything you just did. And then figure out what you have just learned piece by piece since now you have the visual recognition of all those concepts they talked about. I'm sure if you repeat it for couple of projects you'll have higher self-confidence to dive deep more into the world of programming.

In this piece I have compressed the concepts of Object Oriented Programming(OOP) to it's simple form as possible. So, what is Object Oriented Programming(OOP)? OOP is simply a set of rules or programming techniques to design computer programs. Programs that are written in objects(data fields) which contains classes(methods) to describe the object's behavior. Confused already? I have described classes and objects below in a more simple and understandable way. So, let's continue. OOP is programming based around classes and instances of those classes(OBJECTS). OOP revolves around how classes interact and directly affect one another. Concepts of OOP are Abstraction, Encapsulation, Inheritance and Polymorphism (these are very important terms).


Before going into concepts of OOP we also have to understand Class, Objects and Module:

Class

It is a blueprint of what we want to do. Example: Consider an urban planner Mr. X, who is given the contract to plan and build two bedroom house. Urban Planner prepares a blueprint for building a house containing two bedrooms. With the help of blueprint urban planner can develop as many concrete houses as he can. These concrete forms are called object of a class(Blueprint was acting like a class showing what a house should look like).

Objects

Objects are basically comprised of three things, State + Behavior + Identity. Considering an example of a hammer. State - the look of a hammer. Behavior - Hammer is used to hit nails. This is the behavior of hammer. Identity - Each hammer is different. There can be smaller hammer and a big hammer with the same look.

Module

Module can be considered as the collection of the blueprint i.e. classes. For example: Let's say we have blueprints of 2 bedroom house, 3 bedroom house and 4 bedroom house. We would be having separate classes for all three types of bedrooms. Collection of all three blueprints(2 bedroom house,3 bedroom house and 4 bedroom house) is called Module.


Now let's get into the concepts of Abstraction, Encapsulation, Inheritance and Polymorphism

Abstraction

Abstraction is the technique for managing complexity by separating interface or interaction details away from lower level implementation details.

  • For example: let's assume we have two classes – ‘horse’ and ‘eagle’. Also, we have 4 global functions that we already wrote, which are Gallop(); Canter(); Fly(); and hunt(); Our compiler is not smart enough to detect that horse cannot fly() or hunt(). This would be an illegal call . Now what can be the solution, solution is ABSTRACTION. Let’s push the functions gallop() and canter() inside the envelope of class horse and push functions fly() and hunt() inside the envelope of class eagle. Now gallop() and canter() belong to class horse only. And fly() and hunt() belong to class eagle only. Thus if the compiler try to use the function fly() with horse class, it would give an error.

Encapsulation

Encapsulation infers two meaning:

Restricting access to certain components.

For example: To protect a garden from the damage that can be caused by public/animal's, owner hired a guard. Now the guard will be protecting the garden from the reach of public and animals.

Somehow combining/bundling/compiling data with functions that operate on that data.

For example: Let us consider we have a data(int x), which can be changed only with the help of write() function. No external function can access variable x, except for write(). Now if the request to change value of variable x is given and passed by write() function, the value of X would be changed.

Inheritance

Inheritance is the ability to reuse the code. It can be considered as extending functionality of one class to other.

  • For example: Suppose we have a class A, containing blueprint of 2 bedroom house. Now we want a blueprint of 3 bedroom house instead of 2. So what we can do? We can use blueprint of 2 bedroom house and can further extend it to add 1 more room. Here, flat of 2 bedrooms can be considered as BASE CLASS and flat of 3 bedrooms can be considered as DERIVED CLASS.

Polymorphism

It means many types or many behavior. In other words , the ability to appear in many forms. IT refers to the language ability to process. More specifically, it is the ability to redefine methods for derived classes.

  • For example: There is a class called shape. Now, with the help of polymorphism we can define several methods for any number of derived class. Class such as circles, triangles, rectangles. Now, it doesn't matter whatever shape an object(because circles, triangles, rectangles have different shapes) is, applying area methods will give the correct results.

To be continued...

HappyLearning :)


Sources:
http://www.cs.cornell.edu/courses/cs3110/2018sp/l/07-modules/notes.html
https://bhawnarya.wordpress.com/2016/02/18/oops-concepts-from-an-eye-of-qatester/
https://stackoverflow.com/questions/1031273/what-is-polymorphism-what-is-it-for-and-how-is-it-used
https://medium.freecodecamp.org/object-oriented-programming-concepts-21bb035f7260
https://www.webopedia.com/TERM/P/polymorphism.html

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!