# Object Oriented Programming Concepts

When one talks about object oriented programming, it’s not just the objects and classes, it’s how you can use them to simplify the process of programming. There are many different concepts or definitions that affect the process of programming and readability of the code. This article will provide you basic information about objects, classes, inheritance, abstraction, composition, encapsulation and polymorphism.

### Object

Any thing that we see is an object. An object can hold any information or data. We can get an object to do some operations. These operations can modify the object and its attributes. An object-oriented program is nothing but a collection of objects. And an object is a collection of data and operations.

Consider a human body as an object. A human body has eye, ear, nose, hands, hair etc. These are all the data of a human body, and are called attributes. An eye attribute can store the information regarding its color. Ear attribute can hold the information regarding its length(you can think of something else). Similarly, other attributes will have some information stored.

Other than holding the information, a human can do some of the operations. These are called as methods. These methods are the daily activities that a human body can perform, like writing, eating, sleeping, running, etc. We define the methods according to the requirement. These are like the functions in the procedural programming technique.

So, an object has attributes and methods. Objects can interact with each other using their methods. But an object does not have a direct access of the data of another object.  Its methods can be accessed, but the data remains hidden.

### Class

So, we have discussed objects in brief, let’s know about class. Objects are instances of class. It means that when you generalize an object you get a class. We considered a human body as an object. When we make a class comprising of these human bodies we get a class human. Let’s take an example.

Class human{
attribute1;
attribute2;
…..
method1;
method2;
...
}
human humanBody1;
human humanBody2;
human humanBody3;
….


So, human is a class and humanBody1, humanBody2 and humanBody3 are its objects.

It is like declaring variables.

int a;
float b;


So, int and float are classes and a and b are their respective objects.

Now, we can’t do everything with the present variables, so it is necessary to declare class. A class can provide a generic template for the problems we are working on. Like you can have a class for birds, vehicles, employees, videos, computers, books, pictures, etc. For each such class, we can declare any number of specific instances for our program. When we define an object for a class, that instance takes all the default values of that class into defining that object. So, before we use the object of a particular class, our class should be defined. It is better if the class is defined in different file and compiled, so that we do not get a compile time error for using our objects. We will see in later chapters how do we use classes in a different file.

Let’s look at some of the other concept of object oriented technology.

### Abstraction

Abstraction in an object allows a person to focus on what needs to be done, rather than how it’s done. It is to say that, we know all the methods that can be applied to an object, but we just do not know how those methods are implemented. This concept is used in managing many large and complex problems in the corporate world.

Abstraction hides details at the design level of the program. It is basically, hiding unwanted details while giving out the most the most essential details. For instance take the case of a car. You know that it has an ignition function and you implement it without knowing the details of the ignition.

### Inheritance

When we derive a new class form an existing class, it is called as inheritance. The object created through inheritance is called as child object. It has all the properties of the parent object. Child object usually has the same behavior as the parent object, adding some of the behavior of its own. Parent classes are more generalized classes. When our aim is to be more specific and we move down to the child class, deriving the properties of parent class. Inherited class is the subclass of the parent class.

There are various types of inheritance. The first is single inheritance where a class inherits the features of one super class. Then there is multiple inheritance, in this a class have more than one super class. Thus, this subclass has multiple parents. When we have multiple sub classes, then it is known as multilevel inheritance. This can be extended to any number of levels.

In the case of hierarchical inheritance, one of the class is inherited by multiple subclasses. That way a generic class can be useful for many other classes. You can consider a vehicle class as the super class. We can have many subclasses as car, bus, airplane, bike etc. These all can inherit the class of vehicle .

At last we have, hybrid inheritance. It is a combination of single and multiple inheritance.

### Composition

In composition, one object is usually a part of another object. It is to say that one object is composed of another object. Consider a human body as an object, now hands, legs are also objects but they do not have any significance unless related to the body. So body is composed of hands, legs, etc. The other term used for the same is aggregation. It is described with the word has a. Human body has an arm, human body has a leg etc. So a human body is an aggregation of arms, legs etc.

### Encapsulation

Encapsulation deals with the program at the implementation level. The idea of encapsulation is hiding internal details of how an object works. It is about protecting the data in an object. Methods of an object can be used to get the information regarding the data of an object. The sole purpose of encapsulation is to hide internal working of an objects from the outside world, so even if we change some implementation, we do not affect the code on the client side. It allows the object to work independently without the interference of other objects.

When we talked about the ignition of a car in abstraction, hiding those technical details is called encapsulation and providing us with only the interface.

### Polymorphism

Polymorphism is made of two words poly means different and morph means forms. So polymorphism means different forms. It allows the object of different types to respond differently to the same function. Consider the example,

Class A{
void func1(){//first fucntion
cout<<”function is called”<<endl;
}
void func1(int x){//second function
cout<<”function is called with 1 parameter”<<endl;
}
void func1(int x, float y){//third function
cout<<”function is called with 2 parameters”<<endl;
}
}


Now, consider an object of Class A as objA. When we call the function with no parameters it returns the first function call, with one integer parameter it return the second function call. If the function is called with an integer and a float numbers as parameters, then the third function is called. Although the name of the functions are the same. This is also known as function overloading.

All of these concepts are used in programming efficiently and writing beautiful line of code. So, it becomes necessary to grasp these concepts. We will use all of these concepts in further programs

Happy Coding.