Subjects

All subjects Django Java Python React Spring Boot JavaScript PHP
Sign Up Free
Question 2 of 10 · Object-Oriented Programming (OOP)
Interview question

What is the difference between a class and an object? Class और object में क्या अंतर है?

Answer
AspectClassObject
DefinitionBlueprint/templateInstance of class
TypeLogical entityPhysical entity
CreatedOnce during codingMultiple times at runtime
MemoryNo memory allocatedMemory allocated when created
Exampleclass Carcar1 = Car()
class Car:
    def __init__(self, brand, model):
        self.brand = brand
        self.model = model
    
    def drive(self):
        return f'{self.brand} {self.model} is driving'

# Class is template, object is instance
car1 = Car('Toyota', 'Camry')  # Object 1
car2 = Car('Honda', 'Civic')   # Object 2

print(car1.drive())  # Toyota Camry is driving
print(car2.drive())  # Honda Civic is driving

# Check type
print(type(car1))  # <class '__main__.Car'>
print(isinstance(car1, Car))  # True
AspectClassObject
DefinitionBlueprintInstance
TypeLogicalPhysical
CreatedOnceMultiple times

Was this answer clear?