OOP

What are Classes and Objects?

Story Highlights
  • Class and Object

Classes and objects are the two main aspects of object-oriented programming.

Look at the below illustration to see the difference between class and objects:

“car” is a class and “TATA, Audi, Volvo” are objects.

A class is a template for objects, and an object is an instance of a class.

When the individual objects are created, they inherit all the variables and functions from the class.

Object

Any entity that has state and behavior is known as an object. For example, a chair, pen, table, keyboard, bike, etc. It can be physical or logical.

An Object can be defined as an instance of a class. An object contains an address and takes up some space in memory. Objects can communicate without knowing the details of each other’s data or code. The only necessary thing is the type of message accepted and the type of response returned by the objects

Example: A dog is an object because it has states like color, name, breed, etc. as well as behaviors like wagging the tail, barking, eating, etc.

An object mainly consists of: 

  1. State: It is represented by the attributes of an object. It also reflects the properties of an object.
  2. Behavior: It is represented by the methods of an object. It also reflects the response of an object to other objects.
  3. Identity: It is a unique name given to an object that enables it to interact with other objects.
  4. Method: A method is a collection of statements that perform some specific task and return the result to the caller. A method can perform some specific task without returning anything. Methods allow us to reuse the code without retyping it, which is why they are considered time savers. 

Class

Collection of objects is called class. It is a logical entity.
A class can also be defined as a blueprint from which you can create an individual object. Class doesn’t consume any space.

Show More

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button
History Of 1st July Get ready for the ICC Men’s Cricket World Cup 2023! Yoga according to the Bhagavad Gita