OOP

Object Oriented Programming

Posted on Updated on

What is an object?

Object is an entity or things we see around us. It could be a car, person,tree, bird,planet,movie,book, an instrument, an animal and there are so many to mention.

Each object has its attributes and behavior. If we take example of car as object,its attributes could be color,model, make,engine type (petrol or diesel) ,power (2500CC or 3000CC) and transmission (Manual or Automatic), speed,number of doors,etc.

Car has common behavior like start,stop,speed up, speed down , lights on , light off, wipers on/off etc.

If we take another example of a person as an object, attributes could be first name,last name,age,height,weight,address,profession etc.
Person has behavior like walking, talking, eating, drinking, running,etc.

Objects with similar attributes and behavior are categorized into a specific class.
For example,a person belong to human class. Computer belong to electronic class.

What is a Class?

Class is a category or design or template which contains information about attributes (data) and behavior (method)
For example, a person belongs to human class , Orange belongs to fruit class and elephant belongs to animal class.

Object is created from class, so class must exist before object is generated or instantiated.

Before a building is built, the architect prepares a plan and that plan is a class and the building is an object.

So basically class is a unit which describe attributes and behavior.

Technically we can say that class is a factory of objects, since one or many objects  can be instantiated or generated from the same class.

What is Object oriented programming?

In object oriented programming , classes are defined for creating objects and these objects store data (attributes) and has methods/functions(behavior) that operate on attributes (data).Later these objects are used in programming instructions for achieving expected output.

For example, we can define Square class with attribute as side and calculate Area as behavior so that this behavior operates on side (side * side) for calculating area of square.

Lets take another example, suppose we would like to write OOP program for calculating sum of the two numbers. In that case,first we need to define one class by the name say “Calc” with data as FirstNumber and SecondNumber and function by the name
say “Input” that would accept two numbers from end user through keyboard and one more function by the name “Add” and this function would add FirstNumber and SecondNumber and calculate the sum.