Difference Between Class and Object ?
What is a Class?
A class is a blueprint or template used to create objects. It defines the data (fields) and behavior (methods) that the objects will have. A class itself does not occupy memory until an object is created.
C# Example of a Class:
class Car
{
public string Color;
public int Speed;
public void Drive()
{
Console.WriteLine("The car is driving.");
}
}
What is an Object?
An object is an instance of a class. It is the real, usable entity created from the class. An object occupies memory and stores actual values for the fields defined in the class.
C# Example of an Object:
Car myCar = new Car();
myCar.Color = "Red";
myCar.Speed = 80;
myCar.Drive();
Comparison Table
| Feature | Class | Object |
|---|---|---|
| Definition | Blueprint or template | Instance of a class |
| Memory | Does not occupy memory | Occupies memory |
| Purpose | Defines structure and behavior | Holds real data and performs actions |
| Example | Car | myCar, car1, car2 |
| Type | Logical entity | Physical entity (in memory) |
Simple Analogy
A class is like a recipe for a cake.
An object is the actual cake made using that recipe.
One recipe can make many cakes, each different in flavor or decoration—just like many objects can be created from one class.

No comments:
Post a Comment