Object Oriented Programming – An Introduction

Object Oriented programming is one of the most solid and useful way of coding in which we perform actions by using the Objects. For a very basic understanding of Objects and before i start discussing anything else about it let us see what Steve Jobs says about this way of programming-

“Objects are like people. They’re living, breathing things that have knowledge inside them about how to do things and have memory inside them so they can remember things. And rather than interacting with them at a very low level, you interact with them at a very high level of abstraction, like we’re doing right here.

Here’s an example: If I’m your laundry object, you can give me your dirty clothes and send me a message that says, “Can you get my clothes laundered, please.” I happen to know where the best laundry place in San Francisco is. And I speak English, and I have dollars in my pockets. So I go out and hail a taxicab and tell the driver to take me to this place in San Francisco. I go get your clothes laundered, I jump back in the cab, I get back here. I give you your clean clothes and say, “Here are your clean clothes.”

You have no idea how I did that. You have no knowledge of the laundry place. Maybe you speak French, and you can’t even hail a taxi. You can’t pay for one, you don’t have dollars in your pocket. Yet I knew how to do all of that. And you didn’t have to know any of it. All that complexity was hidden inside of me, and we were able to interact at a very high level of abstraction. That’s what objects are. They encapsulate complexity, and the interfaces to that complexity are high level.”

So from this great explanation and example I think most of the concepts of oops must already be crystal but lets discuss some more about it.

oops.
oops.

Oops revolves around few basic given concepts-

  • Objects
  • Classes
  • Inheritence
  • Polymorphism
  • Abstraction
  • Encapsulation

Now you first question must be what are all these terms and what are they used for. So let us discuss them one by one.

What is an Object?

objects
objects

If I ask you to see around yourself and start chanting out the names of things, I am sure you would be able to identify a lot of things and all these things are the objects. So object is anything that has its physical existence and has some attributes and behaviours. 

One important point to remember is that just like if I somehow arrange two different cars of same model with same number plates they will still remain two different cars, similarly even if two or more objects posses same attributes and behaviuor they will still remain different objects.

For Example- pen, chair, table, television, remote etc.

What is a Class?

A blueprint or a prototype with help of which any object is created is known as class. I know it’s a bit unclear so do you remember a while ago i just asked you to think about two same cars of the same model.. Although those objects were different but the prototype or the blueprint which was used to make them must be the same one and that blueprint is nothing but the class of object car of that particular model.

Read more-: How to use Objects and classes in Java??

What is inheritence?What is Polymorphism?

It is a technique through which we manage our code and create a heirarchy in the classes.

in it we have-

Shapes Inheritance
Shapes Inheritance
  • A Base class( OR a Parent class OR a Super class)
  • And a derived class(or a Child class or a Sub class)

You can any of the combination but I prefer Base and Derived class as it is perfect according to the concept name i.e. inheritence because when you use Parent- Child notation it feels like child class must be a smaller one but actually child class has additional features along with the features of parent class.

Inheritence is a way of using the attributes and behaviour of a base class or a Parent class into a derived or a child class just like the traits of parents passed on to their children.

What is Polymorphism?

Lets go to the base – Polymorphism word comes from ancient Greek where poly means many so polymorphic are something which can take many form.

polymorphism is used when we want a task to be performed in a multiple ways like we can provide an “add” method with two parameters to add 2 numbers and one with 3 parameters to add 3 numbers and it is defined at some other time which one to call.

Polymorphism is implemented by 2 ways-

  1. Overloading( Compile time polymorphism)
  2. Overriding( Run time polymorphism) – It is used with the concept of Inheritence.

What is Abstraction?

abstraction-perspective
abstraction-perspective

Another good feature of OOPS is that we can hide the complex and unnecessary details of any object and can display only the required details which are necessary to be displayed so that a person can use that object. If I ask you that do you know what happens when you click the ON button on your TV remote, or What is the internal operations when you change the gear of your motor vehicle, Or whe

n you call using your mobile phone. Most of you will answer NO for atleast one of them and this is the abstraction i.e. providing an abstract view of the complex logic or architecture.

By using this we generally expose our API so that the user can do operations and can use it without caring about how actually we have written our logic.

What is Encapsulation?

Encapsulation
Encapsulation

Binding (or wrapping) code and data together into a single unit is known as encapsulation. In this we protect our variables and methods from the outside world so that there is very little or no effect on the rest of the programs if the internal code of the class changes. It also manages the code effectively by placing the related code, attributes and properties at the same place.

For example: capsule, it is wrapped with different medicines.

A java class is the example of encapsulation. Java bean is the fully encapsulated class because all the data members are private here.