In this lesson we will go over classes and objects. Classes & objects are at the heart of Object-Oriented Programming.
What are Objects?
Object-Oriented programming is a way to model the real world into computer programs. To understand objects take a look around you and see what all things are there. Maybe you see a chair, a table, a bottle, a book, a cell phone, a car, a cat, etc. Each one of these is an object. So, we can define an object as an entity having a specific identity, specific characteristics and specific behavior.
Let’s take the example of bottle and try to understand this definition. It may be a glass bottle or a plastic bottle, it may be filled or empty. It will have some colour. These are some of the characteristics of the bottle. Looking at the behaviour of the bottle, it lets you fill itself with water or juice or for that matter any liquid. It lets you pour out water from it, it lets you close its cap. The bottle belongs to someone, it may be your bottle or your friends’ bottle or your parents’ bottle which gives the bottle an identity.
When you represent an object in your program, variables are used to represent the characteristics of the object and functions are used to represent the behavior. The state of the object is represented through the value of its characteristics. In our bottle example whether the bottle is full or empty or partially filled can be determined by the value of its characteristic.
What are Classes?
Now let’s look at classes. A class is a blue print that represents a set of objects that share common characteristics and behaviour. Let's try to understand this definition with the example of a house.
An architect will have the blueprints for a house. Those blueprints will be plans that explain exactly what properties the house will have and how they are all laid out. However, it is just the blueprint, you can't live in it. Builders will look at the blueprints and use those blueprints to make a physical house. They can use the same blueprint to make as many houses as they want. Each house will have the same layout and properties. Each house can accommodate its own families. So one house might have the Tatas' live in it, one house might have the Ambanis' live in it.
The blueprint is the class, the house is the object and the people living in the house are data stored in the object's properties.