Oop 6
Oop 6
BY : Habtamu Girum
email: Email Address
linkedin : Linkedin
Introduction to OOP
What is OOP? Object-Oriented Programming is a programming paradigm that organizes code around "objects" rather
than functions or procedures. Objects are instances of classes, which act as blueprints defining the properties
(attributes) and behaviors (methods) of those objects.
blueprint objects
Why OOP?
Modularity: OOP groups related data and functions together, making code easier to
understand and maintain.
Reusability: You can reuse code through inheritance, reducing redundancy.
Scalability: Large projects are easier to manage with OOP because of its structured
approach.
Flexibility: Polymorphism allows objects to be treated uniformly, even if they behave
differently.
Classes and Objects
Class: A class is a template or blueprint for creating objects. It defines attributes (data) and methods
(functions) that the objects will have.
Object: An object is an instance of a class, created from the class blueprint. Each object can have its
own values for the class’s attributes.
Syntax:
Class definition: class ClassName:
Object creation: obj = ClassName()
Key Points:
The __init__ method (constructor) initializes an object’s attributes.
The self parameter refers to the current object, allowing access to its attributes and methods.
Example
A class attribute is a property that belongs to the class itself and is shared
among all instances of that class. It is defined within the class body and
accessed using the class name. Class attributes are used to maintain
values that remain the same for all objects created from the class.
Getters and Setters: The @property decorator allows you to define methods that act like
attributes, providing controlled read/write access with validation or logic.
Example
Encapsulation, getter, and setter methods work together to
protect data within a class by controlling access to and
modification of its internal variables.
Getter methods are used to retrieve values, while setter
methods allow controlled modification and validation of
those values.
Getter and Setter Methods:
Getter:
A public method (or property in some languages) that allows external access to the value of a class's
internal variable (field).
It returns the current value of the variable.
It can also be used to perform calculations or transformations on the value before returning it.
Setter:
A public method (or property) that allows modification of the value of a class's internal variable.
It takes a new value as input and assigns it to the variable.
Validation: Setters are often used to include validation logic, ensuring that only valid values are
assigned to the variable.
In this example, _value is Example @property setter and getter
considered a protected attribute
(indicated by the single
underscore prefix).
The @property decorator is used
to define the getter, setter, and
deleter methods for the value
attribute.
This allows controlled access and
modification of the underlying
_value attribute, enabling
encapsulation and data
validation.
@<property_name>.setter
Inheritance:
Python doesn't support traditional method overloading like Java or C++ does.
Why This Doesn't Work in Python
Python doesn't support compile-time method overloading where you can have multiple methods with the same name but different parameter lists.
In Python:
Last Definition Wins: When you define multiple methods with the same name, the last one defined overwrites the previous ones.
Dynamic Nature: Python is dynamically typed, so it can't determine which method to call at compile time based on parameter types.
Run-time Polymorphism
Run-time polymorphism, also known as dynamic polymorphism, is resolved during the execution of the program.
The specific method to be called is determined at run time based on the actual type of the object.
How It Works: Achieved through method overriding, where a subclass provides a specific implementation of a
method defined in its parent class. The method called depends on the object’s type at run time, not the reference
type.
Common Mechanism:
Method Overriding: A subclass redefines a method from the parent class with the same name and signature,
providing a different implementation.
Often used with inheritance and a common parent class or or interface (e.g., an abstract base class).
Python fully supports run-time polymorphism through inheritance and method overriding.
Example-Method overloading
Example - Method Override
Example - Method Overriding abs..
abstract
In Python, abstraction is a key concept in object-oriented
programming (OOP) that focuses on hiding complex
implementation details and exposing only essential information to
the user.
It allows for the creation of simplified models of real-world
entities, making code more manageable and easier to understand.
Abstraction is achieved through abstract classes and methods.