Sunday 2-5 Notes
Sunday 2-5 Notes
- Encapsulation
-Definition:
==> Encapsulation is the process of wrapping data (variables) and code (methods)
together as a single unit (class), and restricting access to some of the object's
components.
Key Points:
Example:
class Student:
def __init__(self, name):
self.__name = name # private attribute
def get_name(self):
return self.__name
2. Inheritance
Definition:
Inheritance allows a class (child/subclass/Derived) to acquire properties and
methods from another class (parent/superclass/Base).
Key Points:
Example:
class Animal:
def speak(self):
print("Animal speaks")
class Dog(Animal):
def bark(self):
print("Dog barks")
3. Polymorphism
Definition:
Polymorphism means "many forms". It allows the same method to perform different
behaviors depending on the object or context.
Types:
Example:
class Animal:
def sound(self):
print("Animal sound")
class Cat(Animal):
def sound(self):
print("Meow")
4. Abstraction
Definition:
Abstraction means hiding complex implementation details and showing only the
essential features of the object.
Key Points:
Example:
class Shape(ABC):
@abstractmethod
def area(self):
pass
class Circle(Shape):
def area(self):
return 3.14 * 5 * 5
Preapred By:
Faqeha Noor