(Source : ChatGPT)
Here are the OOP concepts and their associated keywords in Python, along with some additional related concepts:
Class – a blueprint for creating objects that defines a set of attributes and behaviors. Classes are defined using the class
keyword.
Object – an instance of a class. Objects are created from classes using the class constructor.
Attribute – a value stored in an object, also known as an instance variable.
Method – a function that is associated with an object and can access and modify the object’s attributes.
Encapsulation – the concept of hiding the internal implementation details of an object and only exposing a public interface. Encapsulation is achieved in Python by using the self
keyword and prefixing instance variables with an underscore to indicate that they are private.
Inheritance – a mechanism that allows you to define a new class based on an existing class, inheriting all its attributes and behaviors. Inheritance is achieved in Python using the class DerivedClass(BaseClass):
syntax.
Polymorphism – the ability for objects of different classes to respond to the same method call in a way that’s appropriate for their individual class. Polymorphism is achieved in Python through inheritance and method overriding.
Method Overriding – the ability for a subclass to override a method in its parent class and provide a new implementation. Method overriding is achieved in Python by defining a method with the same name in the subclass.
Abstraction – the process of hiding complex implementation details behind a simple public interface. Abstraction is achieved in Python through encapsulation, inheritance, and method overriding.
Property – a special type of attribute that’s defined using the @property
decorator. Properties are dynamic values that are calculated each time they’re accessed, instead of being stored as static values.
Decorator – a special type of function that modifies or “decorates” another function or class. Decorators are applied using the @decorator_nam
e syntax.
Wrapper Function – a function that wraps or extends the behavior of another function. Wrapper functions take an existing function as an argument and return a new function that adds additional behavior to the original function.
following examples with comment for the concept above
class Animal:
# Constructor: sets the initial values of the object's attributes
def __init__(self, name, species):
self.__name = name # Encapsulation: instance variable is private, indicated by double underscore
self._species = species # Encapsulation: instance variable is protected, indicated by single underscore
# Method: returns the animal's name
def get_name(self):
return self.__name
# Property: returns the animal's species
@property
def species(self):
return self._species
# Method: sets the animal's species
@species.setter
def species(self, species):
self._species = species
# Method: returns a string representation of the object
def __str__(self):
return f"{self.__name} is a {self._species}"
# Inheritance: creates a new class based on the Animal class
class Dog(Animal):
# Constructor: sets the initial values of the object's attributes
def __init__(self, name, breed):
super().__init__(name, species="Dog") # Calls the parent class's constructor
self._breed = breed
# Property: returns the dog's breed
@property
def breed(self):
return self._breed
# Method: sets the dog's breed
@breed.setter
def breed(self, breed):
self._breed = breed
# Method Overriding: provides a new implementation of the parent class's method
def __str__(self):
return f"{self.get_name()} is a {self._species} of breed {self._breed}"
# Polymorphism: objects of different classes can respond to the same method call
def print_animal(animal):
print(animal)
# Object creation: creates a new instance of the Animal class
cat = Animal("Fluffy", "Cat")
# Object creation: creates a new instance of the Dog class
dog = Dog("Buddy", "Labrador")
# Polymorphism: objects of different classes can respond to the same method call
print_animal(cat)
print_animal(dog)
# Output:
# Fluffy is a Cat
# Buddy is a Dog of breed Labrador
Code language: Python (python)
It can be summarize using following formula
Class = Blueprint + Attributes + Methods
Object = Instance of Class + Unique Attributes + Unique Methods
Inheritance = Reuse + Extension of Parent Class
Polymorphism = Same Method Name + Different Implementation
Encapsulation = Data Abstraction + Data Hiding
Now, here is children song
(Verse 1) Classes are like blueprints, you see For objects, just like you and me With attributes and methods too We’ll make our own, it’s up to you!
(Chorus) Objects are special, that’s for sure With unique features and things to do Inheritance, polymorphism, encapsulation These are the words we’ll use and sing with passion!
(Verse 2) Inheritance is like a family tree A parent class, and child class, you’ll see The child class gets all that’s good And adds more, just like it should!
(Chorus) Objects are special, that’s for sure With unique features and things to do Inheritance, polymorphism, encapsulation These are the words we’ll use and sing with passion!
(Verse 3) Polymorphism is like a magic spell One method name, but different tales to tell Depending on the context, you see It does different things, just like magic, can’t you believe?
(Chorus) Objects are special, that’s for sure With unique features and things to do Inheritance, polymorphism, encapsulation These are the words we’ll use and sing with passion!
(Bridge) Encapsulation is like a secret hideaway Data inside, we keep it safe to play No one can touch or change our stuff Our objects are protected, that’s enough!
(Chorus) Objects are special, that’s for sure With unique features and things to do Inheritance, polymorphism, encapsulation These are the words we’ll use and sing with passion