Object Oriented Programming Concepts

Hi Friends!

The reason I have chosen this topic is, these are the basic concepts to any programming language and most frequently asked during the interviews. I have tried to simplify the concept and examples so that anyone could connect.

Object Oriented Programming (OOP) is a programming model where programs are organized around objects and data rather than action and logic. Action and logic revolves around the Procedural Programming.
OOP allows us to break a problem into a number of small units called objects and then builds data and functions around these objects.

  • The software is divided into a number of small units called objects. The data and functions are built around these objects.
  • The data of the objects can be accessed only by the functions associated with that object.
  • The functions of one object can access the functions of another object.

For example, my grandma loves to bake cupcakes. So I would take several cupcake shape cutters, for instance I would take a square shape cutter and make several imprints that resembles the square shape.
So this square cupcake cutter acts like a class, it’s basically like a blueprint, that creates several objects to in this case square cupcakes.
Now grandma knew that grandpa and my sister likes cupcakes coated with chocolate. So she would modify few square shaped cupcakes into chocolate coated cupcakes. Well this is how overriding works in OOP.
But mom likes cupcakes that are of round shape, now grandma would take round cutter which has the same diameter as the side of the square shape cutter and would make round shaped cupcakes. Therefore you see if you change the shape of the cupcake, it doesn’t affect the shape of other cupcakes. Well this is how OOP ensures code protection.

We will understand the concepts covered in this example in details:

Basic concepts of Object Oriented Programming:

It is necessary to understand some of the concepts used majorly in object oriented programming. This include:

Objects: objects are the basic run-time entities in an object-oriented system. They may represent a person, a place or any item that the program has to handle. Programming problem is understood based on number of items and the communication between them.
All objects have a state and a behavior. The state of the object in a program is representing using data members/variables and behavior is represented using functions/methods. Example: A car has states – model, year of manufacturing, color as well as behaviors – starting the car, moving it by driving and stopping it.“An object is an instance of a class.”
If you compare the software object with a real-world object, they have very similar characteristics.
A class will not occupy any memory space. Hence to work with the data represented by the class you must create a variable for the class that is called an object.

Class: We just mentioned that objects contain data, and code to manipulate that data. The entire set of data and code of an object can be made a user-defined data type with the help of a class. Class is the template/blueprint that defines the characteristics of a certain class of object. 
A class defines both the interface and the implementation for a set of objects, which determines their behavior. It contains variables for storing data and functions to perform operations on the data. A class will not occupy any memory space and hence it is only a logical representation of data. Example, mango, apple and orange are members of the class fruit.

Encapsulation: The wrapping up of data and functions into a single unit (called class) is known as encapsulation. Data encapsulation is the most striking feature of a class. Say we have a program, it has a few logically different objects which communicate with each other – according to the rules defined in the program. Encapsulation is achieved when each object keeps its state private, inside a class. Other objects don’t have direct access to this state. Instead, they can only call a list of public functions – called methods.
So, the object manages its own state via methods – and no other class can touch it unless explicitly allowed. If you want to communicate with the object, you should use the methods provided. But (by default), you can’t change the state. Let’s say we’re building a tiny cat game. There are people and there is a cat. They communicate with each other. We want to apply encapsulation, so we encapsulate all “cat” logic into a cat class. It may look like this:
You can feed the cat. But you can’t directly change how hungry the cat is.

Here the “state” of the cat is the private variables mood, hungry and energy. Cat has the behavior of making noise and game has a private method meow() to control the same. It can call it whenever it wants, the other classes can’t tell the cat when to meow.
The cat has some behaviors which are accessible by outsiders and are defined in the public methods sleep(), play() and feed(). Each of them modifies the internal state somehow and may invoke meow(). Thus, the binding between the private state and public methods is made.

Abstraction: It can be thought of as a natural extension of encapsulation. Abstraction refers to the act of representing essential features without including the background details or explanations. 
Abstraction is a concept aiming to ease this problem. Applying abstraction means that each object should only expose a high-level mechanism for using it. This mechanism should hide internal implementation details. It should only reveal operations relevant for the other objects.
Abstraction lets you focus on what the object does instead of how it does it. Abstraction provides you a generalized view of your classes or objects by providing relevant information. It is the process of hiding the working style of an object, and showing the information of an object in an understandable manner.
For example: If I am your laundry object, you can give me your dirty clothes and send me a message that says, “Can you get my clothes dry cleaned please.” I happen to know where the best dry cleaners in Mumbai. And I can speak Marathi, and I have money in my pockets. So I go out and take an auto and tell the driver to take me to this place in Mumbai. I go get your clothes dry cleaned, I jump back into the auto and 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 dry cleaners place. Maybe you speak English, and you can’t even board an auto. You can’t pay for one, you don’t have money 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 high level of abstraction. And that’s what objects are. They encapsulate complexity, and the interfaces to the complexity are high level.

Difference between Encapsulation and Abstraction:

Encapsulation Abstraction
Encapsulation solves the problem in the implementation level. Abstraction solves the problem at the design level.
Encapsulation means hiding the code and data into a single unit to protect the data from the outside world. Abstraction hides unwanted data and provides relevant data.
Encapsulation means hiding the internal details or mechanics of how an object does something. Abstraction lets you focus on what the object does instead of how it does it.
Encapsulation- Inner layout, used in terms of implementation.
For example: the internal details of a Mobile Phone, how the keypad button and display screen are connected with each other using circuits.
Abstraction means Outer layout, used in terms of design.
For example: An external of a Mobile Phone, like it has a display screen and keypad buttons to dial a number.

Inheritance: Inheritance is a process by which objects of one class acquire the properties of objects of another class. It supports the concept of hierarchical classification. For example, consider human being we all have characteristics that identifies us as individuals like names, birth dates, mobile number, email id, qualification, profession etc. If we separate the common aspects as human being in class Person (as shown in below diagram), and the profession into different classes as Teacher and Student, make the working and understanding easy. Here when we create the object of the teacher class all the private variables and methods from the Person class will be available for use. Class Person will be called as parent class and class Teacher and class Student will be called as child/derived class.
In OOP, the concept of inheritance provides the idea of reusability. This means that we can add additional features to an existing class without modifying it. For example: if we want to define two types of teachers, one who take private tuitions and second who teaches in the school. This is possible by deriving a new class from the existing one. We have achieved the same by deriving two classes, class Private Teacher and class Public Teacher from the parent class Teacher.
In the above diagram if we concentrate only on class Person, Teacher and Student, it become Single Inheritance. It means we have a parent class and multiple child classes, example: your parents and you and your siblings are single generation of the family, it is similar to single inheritance.
In the similar way, if we consider the whole diagram, class Person is similar to your grandparents, class Teacher is similar to your parents and class Private Teacher and Public Teacher are you and your siblings. This is two generations of the family, it become Multilevel Inheritance.

Polymorphism: Polymorphism, a Greek term, means the ability to take more than one form. An operation may exhibit different behaviors in different instances. The behavior depends upon the types of data used in the operation. For example, consider your mother, human being and she is a working in a school as a vice principal and teaches the students, lets understand the different roles played by her; She is a human being, a teacher to her students, a vice principal to her fellow colleagues/teachers, a mother to you, a wife to your father, a daughter-in-law/daughter to your grandparents, a sister to your uncle and aunt etc.
Polymorphism is a concept, which allows us to redefine the way something works, by either changing how it is done or by changing the parts using which it is done. Both the ways have different terms for them. If we walk using our hands, and not legs, here we will change the parts used to perform something. Hence this is called overloading. And if there is a defined way of walking, but I wish to walk differently (cat walk, brisk walk), but using my legs, like everyone else. Then I can walk like I want, this will be called as overriding.

Let’s summarize all the concepts of object oriented programming using an example:

  • You live along with your family members in a house, the house is a ‘class’.
  • Every member has particular task to do; the tasks are ‘methods/functions’.
  • Your apartment / building is a ‘package’ that has many houses (classes).
  • You all members of your house are ‘variables’ who live safe in your house, outsiders cannot see/call you directly; that’s encapsulation.
  • You and your father have same eating habit and style, that’s inheritance.
  • You, a child, your name is Akshay who is a son at home, student at school, player at the ground; everywhere you will be called with the same name “Akshay” but you are being called to perform different tasks at those different places, that's what you call ‘Polymorphism’.

Hope this article was useful to you guys, please leave you comments and feedback.
Happy reading!

Comments