UNIT-I
Object Oriented Programming
The goals of Object-Oriented Programming (OOP) in Python, particularly focusing on
Robustness, Adaptability, and Reusability, can be understood as follows:
Robustness:
This refers to the ability of software to handle errors and unexpected inputs gracefully,
ensuring its correctness and reliability. In Python OOP, this is achieved through:
Encapsulation: By bundling data and methods within classes, encapsulation
helps protect data from external, unauthorized access, reducing the likelihood of
errors caused by unintended modifications.
Modular design: Breaking down a complex system into smaller, independent
objects and classes makes it easier to identify and fix issues within specific
modules without affecting the entire system.
Adaptability:
This emphasizes the software's capacity to evolve and accommodate changes in its
environment or requirements over time. OOP in Python supports adaptability through:
Inheritance: Allows for the creation of new classes (child classes) that inherit
attributes and methods from existing classes (parent classes), enabling easy
extension and modification of functionalities without altering the original code.
Polymorphism: Enables objects of different classes to be treated as objects of a
common type, promoting flexibility and allowing for easy adaptation to new data
types or behaviors.
Reusability:
This focuses on designing code components that can be used in different parts of an
application or in various projects, saving development time and effort. In Python OOP,
reusability is facilitated by:
Classes and Objects: Defining classes as blueprints allows for the creation of
multiple instances (objects) with similar properties and behaviors, promoting code
reuse.
Inheritance: As mentioned, inheritance directly promotes reusability by allowing
child classes to leverage the code defined in parent classes.
Modularity: Well-defined, independent objects and classes can be easily
integrated into different systems or applications, enhancing code reusability
across projects
Principles of OOP
Object-Oriented Programming Principle is the strategy or style of developing applications
based on objects.
Object-Oriented Principles mainly include the 4 pillars that together make the OOP a very
powerful concept. That is –
1. Abstraction
2. Encapsulation
3. Inheritance
4. Polymorphism
1. Abstraction
Abstraction can be defined as hiding internal implementation and showing only the
required features or set of services that are offered. This is the most essential part of
Object-Oriented programming.
Features of Abstraction –
Security- With Abstraction, the Outside persons don’t know the internal
implementation so it makes the classes more secure, so the unauthorized user will not
have access to the data.
Easy Enhancement- Suppose in the future we want to change the logic of the
implementation in class, So we don’t have to change the entire external logic which is
there for the user. Just we need to make changes for the methods and it will not affect the
functionality.
Improves Easiness- It helps to use the features without knowing implementation so
it improves easiness for the users to use that.
Maintainability will improve- Without affecting the user, it can able to perform
any types of changes internally. SO maintainability will improve.
2. Encapsulation:
Encapsulation can be defined as the binding of data and attributes or methods and data
members in a single unit.
In classes, we have Data and attributes that perform operations on that data. So
according to the OOPs principle of Encapsulation, that data can be merged into a single
unit.
Encapsulation enhances more security of the data as everything related to a single
task must be grouped and access to the data is given as per need. And this can be
achieved using the concept of Data Hiding.
Encapsulation = Data Hiding + Abstraction.
Data Hiding – It means hiding the data of the class and restricting access to the
outside world. Example – Using the access specifier keywords like private that restricts
the data to only accessible and modifiable in the same class. Outside users can not access
the data.
3. Inheritance
Inheritance is the method of acquiring features of the existing class into the new class.
There are two keywords involved in the inheritance – Base and Derived Class.
Base Class – It is also termed the parent class. It is the main class that has the basic
properties and methods which is defined.
Derived Class – This is the extension of the base class and it is also called the child class.
It has the properties and methods that are in the base class with its own features in it.
4. Polymorphism
Polymorphism is the most essential concept of the Object-Oriented Programming principle.
It has meaning ‘poly’ – many, ‘morph’ – forms. So polymorphism means many forms.
In Object-Oriented Programming, any object or method has more than one name
associated with it. That is nothing but polymorphism.
OOPs states 2 types of Polymorphism –
1. Compile Time Polymorphism(Static binding)
2. Runtime Polymorphism(Dynamic binding)
Compile Time Polymorphism is achieved using Method Overloading.
Method Overloading – When more than one method is declared with the same
name but with a different number of parameters or different data-type of parameter, that
is called method overloading.
Runtime polymorphism is achieved using Method Overriding.
Method Overriding – When more than one method is declared as the same name
and same signature but in a different class. Then the derived class method will override the
base class method. This means the Base class method will be shadowed by the derived
class method. And when the method is called then it can be called based on the overridden
method.