1. What is a Class?
A class is one of the basic units necessary for supporting object-oriented programming (OOP) in programming languages. A class is used to bundle data (attributes) and methods (functions) for manipulating that data together. Using classes allows you to organize the structure of your code well and make it more reusable.
2. Difference between Class and Object
A class is a blueprint (design) for creating objects. An object is an entity created based on a class, which occupies memory at runtime and has a state that can act.
3. Defining a Class
A class is defined using the class
keyword. Class names usually start with an uppercase letter, and the body of the class should be indented.
class Car:
def __init__(self, make, model, year):
self.make = make
self.model = model
self.year = year
4. Constructor and Destructor
A constructor is a method that is automatically called when an object is created, and it is mainly used to set the initial state of the object. In Python, the __init__
method serves this purpose. A destructor is called when an object is deleted and can be defined with the __del__
method.
5. Class Variables and Instance Variables
A class variable is a variable that is shared by the class, having the same value for all instances. An instance variable is a variable that can have separate values for each object.
class Car:
number_of_wheels = 4 # Class variable
def __init__(self, make, model, year):
self.make = make # Instance variable
self.model = model # Instance variable
self.year = year # Instance variable
6. Defining Methods
A method is a function defined within a class that defines the behavior of the object. Instance methods are usually used, and the first parameter is self
, which allows access to the object itself.
class Car:
def start_engine(self):
print("Engine started")
7. Inheritance
Inheritance is a technique for creating new classes based on existing classes, which increases code reusability. It is defined in the form class DerivedClass(BaseClass):
.
class ElectricCar(Car):
def __init__(self, make, model, year, battery_size):
super().__init__(make, model, year)
self.battery_size = battery_size
8. Polymorphism
Polymorphism is the ability to handle different data types with the same interface. In Python, polymorphism can be implemented through method overriding.
class Animal:
def sound(self):
pass
class Dog(Animal):
def sound(self):
return "Woof!"
class Cat(Animal):
def sound(self):
return "Meow!"
9. Encapsulation
Encapsulation means restricting outside access to some of the implementation details of an object. In Python, it is conventionally indicated by prefixing the variable name with an underscore (private) to represent encapsulation.
10. Example of Using Classes
Here, we will create an example that includes all the concepts explained above.
class Book:
def __init__(self, title, author, pages):
self.title = title
self.author = author
self._pages = pages # Private variable
def __str__(self):
return f"{self.title} by {self.author}"
def set_pages(self, pages):
if pages > 0:
self._pages = pages
else:
raise ValueError("The number of pages must be positive.")