0% found this document useful (0 votes)
18 views4 pages

Oops (Object Oriented Programming Structure)

The document discusses object oriented programming concepts in Python including functions, classes, objects, constructors, the self keyword, encapsulation, inheritance, abstraction, and polymorphism.

Uploaded by

bizzpy n
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
18 views4 pages

Oops (Object Oriented Programming Structure)

The document discusses object oriented programming concepts in Python including functions, classes, objects, constructors, the self keyword, encapsulation, inheritance, abstraction, and polymorphism.

Uploaded by

bizzpy n
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 4

Oops(object oriented programming structure)

User define function


a function is a block of reusable code that performs a specific task or a set of tasks. It
allows you to break down your code into smaller, more manageable pieces, making
your code organized, modular, and easier to understand. Functions in Python are
defined using the def keyword followed by the function name, a set of parentheses for
parameters (if any), and a colon. The function body is indented below the def statement.
def function_name(arguments):
Statement

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

Abstraction: Abstraction is the process of hiding the implementation details of an object


and exposing only the essential features or functionalities to the outside world. It allows
you to create a simplified representation of an object, focusing on what it does rather
than how it does it. In other words, abstraction provides a high-level view of an object's
behavior, enabling you to work with complex systems more easily.

Polymorphism: Polymorphism allows objects of different classes to be treated as


objects of a common base class. It provides a way to use a single interface to represent
different types of objects. In other words, polymorphism allows you to perform a single
action in different ways depending on the object's type or class.
Polymorphism can be achieved in Python through method overriding and method
overloading:
1. Method Overriding: When a subclass provides a specific implementation for a
method that is already defined in its base class, it is called method overriding.
The method in the subclass overrides the method in the base class, and the
subclass's method is executed when called on an object of the subclass.
2. Method Overloading: Method overloading allows you to define multiple methods
with the same name in a class, but with different parameter lists. Python does not
support traditional method overloading like some other languages (e.g., Java,
C++), but you can use default arguments or variable-length arguments to achieve
similar behavior

You might also like