Tuesday, November 19, 2019

Object Oriented Programming (OOP) in Layman's Terms

Object Oriented Programming System (OOPs) is a programming model based on the concept of "objects" that contain data and methods.
The primary purpose of object-oriented programming is to increase the flexibility and maintainability of programs.
Object Oriented Programming brings together data and its behavior (methods) in a single location (object) makes it easier to understand how a program works.

Class & Object

First of all, let's see the formal definition of class and object:

A class is a blueprint, or prototype, that defines the variables and the methods common to all objects of a certain kind.
An object is a software bundle of variables and related methods. 

Everything around us is an OBJECT like a car, a pencil, a table, a dog, you and me. Each object belongs to a particular CLASS.



For example, if I ask you to tell me the similarity between dog, cat, lion, zebra then you will say that all of these are animals which means all of them to belong to a class Animal.


Similarly, if I say table, chair, shelf then we can say that these all are furniture. That means all these objects belong to a class Furniture.

We can also say that objects are proper nouns and classes are common nouns.

These classes can be further subdivided into more classes to give more details about the group of objects they contain. For example, the class Animal given above can be further subdivided into two classes as Wild Animals and Domestic Animals and the class Furniture can be subdivided into two classes as Wooden Furniture and Steel Furniture. This is the concept behind inheritance.

Every object has states and behavior. Let’s take an example of a class Animal and its object dog. Now as we all know that a dog has states - name, color, breed etc. and behavior - barking, running, fetching, wagging tail, etc.

These states are called Variables and behavior are called methods or functions. Now we can define these variables and methods in class Animal and can call these methods anytime after creation of its object a dog.

Think of everything as an object and assign classes to them.

Abstraction

You have a bike. In the bike there is a clutch on left hand, front brake on right hand, gears on left leg and rear brake or right leg. Using these four you control your bike while driving. You don’t know how engine internally works when these are applied. You only need to know how to apply these. This is abstraction. And brake, gears , clutch are interfaces.

Internally drum brake will be replaced via disc brake, 100 cc engine will be replaced via 200 cc engine but the interface will remain unchanged. Also, even you switch the bike you will get the same interface (almost).

Encapsulation

Encapsulation can be referred as information hiding. How bike engine is internally implemented, how it interacts with the several parts of the bike like wheel, fuel tank etc. is completely hidden. You can access functionality of the bike only with the interfaces exposed. You can start a bike with the key only. If bike has some flaw that you can even start it without key that we can say that bike is not encapsulated.

Inheritance

There are some properties of bike like gears, clutch, brake. Every bike contains these properties. Hero bike also contains these properties. Bajaj bike also contains these properties. So we can say that Hero is also bike and Bajaj is also a bike. Also Hero is not Bajaj, as it differs than Hero in some mean. This is inheritance. Inheritance is an IS-A property.

Polymorphism

Polymorphism is derived from 2 greek words: poly and morphs. The word "poly" means many and "morphs" means forms. So polymorphism means many forms.



Suppose if you are in class room that time you behave like a student, when you are in market at that time you behave like a customer, when you at your home at that time you behave like a son or daughter, Here one person present in different-different behaviors.



0 comments:

Post a Comment