0% found this document useful (0 votes)
0 views

Deep_OOP_Concepts_Notes_Extended

The document covers advanced OOP concepts in Python, including class creation, method overloading and overriding, data hiding, abstraction, inheritance, and composition. It explains key principles like encapsulation, polymorphism, and code reusability with examples for each concept. The document emphasizes the importance of these concepts in building scalable and maintainable software systems.

Uploaded by

Yash raundal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

Deep_OOP_Concepts_Notes_Extended

The document covers advanced OOP concepts in Python, including class creation, method overloading and overriding, data hiding, abstraction, inheritance, and composition. It explains key principles like encapsulation, polymorphism, and code reusability with examples for each concept. The document emphasizes the importance of these concepts in building scalable and maintainable software systems.

Uploaded by

Yash raundal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Advanced OOP Concepts in Python

5.1 Creating Classes and Objects

1. A class is a user-defined blueprint for creating objects.

2. The class uses the `class` keyword followed by the class name.

3. An object is an instance of a class.

4. Objects access attributes and methods using the dot (`.`) operator.

5. The `__init__` method is the constructor used to initialize object data.

6. The `self` parameter refers to the current object.

7. Attributes can be defined inside the constructor or directly inside the class.

8. Methods are functions defined inside a class.

9. Class variables are shared across all objects; instance variables are specific to each object.

10. Classes promote code reusability and modularity.

Example:

class Student:

college = "MIT Polytechnic" # class variable

def __init__(self, name, roll):

self.name = name # instance variable

self.roll = roll

def display(self):

print(f"Name: {self.name}, Roll: {self.roll}, College: {Student.college}")


Advanced OOP Concepts in Python

s1 = Student("Prince", 101)

s1.display()

5.2 Method Overloading and Overriding

1. Method Overloading: Defining multiple methods with the same name but different parameters.

2. Python does not support traditional overloading but can mimic using default or variable arguments.

3. Use *args or default arguments for flexible function signatures.

4. Method Overriding: Redefining a method in the child class with the same name and parameters.

5. Overriding allows specialized behavior in subclasses.

6. Use `super()` to call the parent class version of the method.

7. Polymorphism is achieved through method overriding.

8. Supports dynamic dispatch (decides method at runtime).

9. Helps implement run-time polymorphism.

10. Promotes flexibility and code extension.

Example:

class Animal:

def sound(self):

print("Animal sound")

class Cat(Animal):

def sound(self):

super().sound()
Advanced OOP Concepts in Python

print("Meow")

5.3 Data Hiding

1. Data hiding restricts direct access to class attributes.

2. In Python, prefixing variables with double underscores makes them private.

3. Private attributes cannot be accessed directly outside the class.

4. Use getter and setter methods to access and modify private attributes.

5. Prevents accidental modification of data.

6. Promotes encapsulation and security.

7. Ensures class control over attribute access.

8. Python internally renames private variables with `_ClassName__varname`.

9. Helps in maintaining the integrity of data.

10. Protects object integrity in large systems.

Example:

class BankAccount:

def __init__(self, balance):

self.__balance = balance

def get_balance(self):

return self.__balance

def deposit(self, amount):


Advanced OOP Concepts in Python

self.__balance += amount

5.4 Data Abstraction

1. Abstraction shows essential features and hides complexity.

2. Achieved using abstract classes and interfaces.

3. Abstract classes in Python are created using the `abc` module.

4. Abstract methods have no implementation in the base class.

5. Subclasses must override abstract methods.

6. Prevents instantiation of abstract classes directly.

7. Promotes interface consistency among subclasses.

8. Separates interface from implementation.

9. Helps in developing scalable systems.

10. Supports hierarchical and layered design.

Example:

from abc import ABC, abstractmethod

class Vehicle(ABC):

@abstractmethod

def start_engine(self):

pass

class Bike(Vehicle):
Advanced OOP Concepts in Python

def start_engine(self):

print("Bike engine started")

5.5 Inheritance and Composition Classes

1. Inheritance allows a class to inherit attributes and methods from another class.

2. Supports code reuse and modularity.

3. Python supports single, multiple, multilevel, hierarchical, and hybrid inheritance.

4. Use `super()` to call base class methods.

5. Child class can override parent class methods.

6. Composition is a "has-a" relationship.

7. In composition, a class contains instances of other classes.

8. Promotes flexibility by combining classes.

9. Use inheritance for "is-a", composition for "has-a" relationships.

10. Both can be used together for powerful designs.

Example:

class Engine:

def start(self):

print("Engine started")

class Car:

def __init__(self):

self.engine = Engine()
Advanced OOP Concepts in Python

def start(self):

self.engine.start()

5.6 Customization via Inheritance Specializing Inherited Methods

1. Customization allows subclasses to redefine or extend behavior.

2. Use method overriding to provide specialized behavior.

3. Helps adapt general behavior for specific needs.

4. Use `super()` to retain and extend parent behavior.

5. Promotes reuse of generic code.

6. Encourages polymorphism and flexibility.

7. Keeps code DRY (Don't Repeat Yourself).

8. Ensures cleaner, maintainable subclasses.

9. Specialized behavior can also include adding new methods.

10. Useful in frameworks and libraries for extending base functionality.

Example:

class Printer:

def print_doc(self):

print("Printing in black and white")

class ColorPrinter(Printer):

def print_doc(self):
Advanced OOP Concepts in Python

super().print_doc()

print("Printing in color")

You might also like