


What are the distinctions between a class and an object in object-oriented programming (OOP)? Discuss their roles, how they interact, and provide examples to illustrate your points.
A class serves as a blueprint for creating objects. It encapsulates data for the object and methods to manipulate that data, defining the properties and behaviors of the objects created from it.
class Dog:
def bark(self):
return 'Woof!'
An object is an instance of a class. It contains actual values and states defined by the class. Each object can hold different data while sharing the same structure and behavior defined by the class.
my_dog = Dog()
my_dog.bark() # Returns 'Woof!'
Instantiation is the process of creating an object from a class. When a class is instantiated, memory is allocated for the new object, and its properties are initialized.
my_cat = Cat() # Cat is another class.
Encapsulation is a principle of OOP that restricts access to certain components of an object. This means that the internal representation of an object is hidden from the outside, exposing only necessary methods.
class BankAccount:
def __init__(self, balance):
self.__balance = balance # Private attribute