Oops (Object Oriented Programming Structure)
Oops (Object Oriented Programming Structure)
Class
A class is a blueprint for creating objects. It is a user-defined data type that
encapsulates data and the methods (functions) that operate on that data. Classes
provide a way to organize and structure code by grouping related attributes (variables)
and behaviors (functions) into a single unit.
To define a class in Python, you use the class keyword followed by the class name and
a colon. The class body contains class variables, instance variables, and methods.
syntax:
class classname:
Statement
Object
An object is an instance of a class. It is a concrete representation of the class blueprint,
and it contains both the data (attributes) and the methods (functions) defined in the
class. When you create an object from a class, it is said to be "instantiated."
In simpler terms, a class is like a blueprint, and an object is a real-world entity built
based on that blueprint. Each object created from the class can have its own unique
data, while still having access to the methods defined in the class.
Constructor :
In Python, a constructor is a special method used to initialize the attributes (data
members) of an object when it is created from a class. The constructor method is
denoted by __init__ (double underscores on both sides), and it is automatically called
whenever a new object is instantiated from the class.
Constructor methods are essential in object-oriented programming as they allow you to
set initial values to object attributes and perform any other setup tasks necessary for the
object to function correctly.
Self keyword:
self is a special keyword that represents the instance of the class (the object) within a
class's methods, including the constructor (__init__). It is a convention in Python to use
the name self, but it is not a strict requirement; you can technically use any other valid
variable name instead of self. However, sticking to the convention makes your code
more readable and easier to understand for others.
When you define a method inside a class, including the constructor, the first parameter
is always self. This parameter acts as a reference to the instance of the class that the
method is being called on. It allows you to access and modify the object's attributes and
call other methods within the class.
Here are the key points to understand about the self keyword:
self Parameter in Method Definition:
● In a class method, the first parameter should be self, though it can be
named differently if desired.
● When you call a method on an object, Python automatically passes the
object as the first argument to the method. This is why you need to define
self as the first parameter in the method definition to capture that
reference.
Accessing Object Attributes:
● Inside a method, you can access the object's attributes using the self
keyword followed by the attribute name (e.g., self.attribute_name).
● By using self, you can distinguish between the attributes of the current
instance (object) and any other variables defined within the method.
Setting Object Attributes:
● You can set the value of an object's attributes inside a method using the
self keyword, making the attribute specific to that particular object.
● For example, in the constructor, you typically set the initial values of the
object's attributes using self.
Encapsulation
Encapsulation is one of the four fundamental principles of object-oriented programming
(OOP), along with inheritance, polymorphism, and abstraction. It is a concept that
allows you to bundle data (attributes) and the methods (functions) that operate on that
data within a single unit (the class). Encapsulation provides a way to hide the internal
implementation details of a class from the outside world, exposing only what is
necessary for the class to interact with other parts of the code.
The main goals of encapsulation are:
● Data Hiding: By making attributes private or protected, encapsulation prevents
direct access and modification of the class's internal data from outside the class.
This helps maintain data integrity and prevents unintended changes to the
object's state.
● Abstraction of Implementation: Encapsulation allows you to present a simple and
clear interface to the outside world while hiding the complexity of the underlying
implementation. This promotes a clear separation of concerns between different
parts of the codebase.
● Code Modularity: Encapsulation allows you to divide the implementation of a
class into smaller, manageable pieces. Each class can be developed and
maintained independently, making the code more organized and maintainable.
Access modifiers
Encapsulation is achieved through the use of access control modifiers, which define the
visibility of class members (attributes and methods). There are three access control
levels in Python:
1. Public: Members are accessible from anywhere outside the class. By default, all
attributes and methods in a class are public.
2. Protected: Members are accessible within the class and its subclasses (derived
classes). To define a member as protected, prefix its name with a single
underscore (e.g., _protected_attribute).
3. Private: Members are only accessible within the class itself. To define a member
as private, prefix its name with two underscores (e.g., __private_attribute). Note
that Python performs name mangling, making the attribute technically still
accessible with a different name, but it is intended to discourage direct access
from outside the class.
Inheritance
Inheritance is one of the key principles of object-oriented programming (OOP), and it
allows you to create a new class (called the derived class or subclass) based on an
existing class (called the base class or superclass). The derived class inherits the
attributes and methods of the base class, promoting code reuse and hierarchical
organization of classes.
The derived class can extend or modify the behavior of the base class by adding new
attributes and methods or by overriding existing ones. This makes inheritance a
powerful tool for creating specialized classes and promoting code modularity.
Key concepts of inheritance in Python:
1. Base Class (Parentclass): The class from which another class inherits is called
the base class or superclass. It serves as the parent class, providing the
common attributes and methods that can be shared with its derived classes.
2. Derived Class (Childclass): The class that inherits from the base class is called
the derived class or subclass. It inherits all the attributes and methods of the
base class and can also have additional attributes and methods specific to itself.
Syntax of Inheritance: To create a derived class, the class statement includes the name
of the base class in parentheses after the derived class name.
Python syntax:-
Class Parentclass:
statement
class childclass(ParentClass):
3. statement