There are four main principals of object oriented programming (oop). These are know as:
Encapsulation = keeping code together in a single unit.
Abstraction = hide the implementation details.
Inheritance = allows a class to inherit the attributes and methods of another class.
Polymorphism = methods with the same names but in different classes.
And are described in the sections below. Click to expand.
In Python, encapsulation is the process of wrapping data and code together in a single unit, typically an object. Encapsulation is a key concept in object-oriented programming (OOP), and it helps to ensure that the internal workings of an object are hidden from the outside world.
There are several ways to achieve encapsulation in Python:
Private attributes: You can use the double underscore notation (__) to define private attributes in Python. Private attributes are not accessible from outside the object, and they can only be accessed or modified using special methods known as accessors and mutators.
For example:
class MyClass:
def __init__(self):
self.__private_attribute = "Private attribute"
def get_private_attribute(self):
return self.__private_attribute
def set_private_attribute(self, value):
self.__private_attribute = value
obj = MyClass()
print(obj.get_private_attribute()) # Output: "Private attribute"
obj.set_private_attribute("New value")
print(obj.get_private_attribute()) # Output: "New value"
In Python, abstraction is a programming technique that is used to hide the implementation details of a class or function and to expose only the essential features to the user. Abstraction is another fundamental principle of object-oriented programming (OOP), and it is often used to create a simplified interface for a complex system.
Here is an example of how you could use abstraction in Python:
class Circle:
''' class for Circle '''
__pi = 3.14159
__pi_eng = 3
def __init__(self, radius:float):
self.radius = radius
return
def area(self):
return self.__pi * self.radius**2
def __hidden_method(self, radius):
area = self.__pi_eng * radius**2
return area
def engineering_area(self):
area = self.__hidden_method(self.radius)
return area
c = Circle(10)
print(c.area())
print(c.engineering_area())
In Python, inheritance is a mechanism that allows a class to inherit the attributes and methods of another class, allowing you to create a new class that is based on an existing one. Inheritance is a fundamental principle of object-oriented programming (OOP), and it is often used to create a hierarchical structure of classes in which a subclass can inherit the characteristics of a superclass.
Here is an example of how you could use inheritance in Python:
class Animal:
def __init__(self, name):
self.name = name
def speak(self):
raise NotImplementedError("Subclass must implement this method")
class Dog(Animal):
def speak(self):
return "Woof!"
class Cat(Animal):
def speak(self):
return "Meow!"
dog = Dog("Fido")
cat = Cat("Fluffy")
print(dog.speak()) # Woof!
print(cat.speak()) # Meow!
In Python, polymorphism is a programming technique that allows a single interface to be used with multiple implementations. Polymorphism is a fundamental principle of object-oriented programming (OOP) and is often used to allow objects of different classes to be used interchangeably.
There are several ways to achieve polymorphism in Python, including inheritance, method overloading, and method overriding.
Inheritance is a mechanism that allows a class to inherit the attributes and methods of another class, allowing you to create a new class that is based on an existing one. This can be used to create a hierarchy of classes, where a subclass is a specialized version of a superclass.
Method overloading is a technique that allows a class to have multiple methods with the same name, but with different arguments. This can be used to provide multiple implementations of a method for different types of input.
Method overriding is a technique that allows a subclass to override the implementation of a method inherited from a superclass with its own implementation. This can be used to provide a specialized version of a method for a specific subclass.
Here is an example of how you could use polymorphism in Python to implement method overriding:
class Animal:
def make_sound(self):
print("Some generic animal sound")
class Dog(Animal):
def make_sound(self):
print("Bark!")
class Cat(Animal):
def make_sound(self):
print("Meow!")
animals:list[Animal] = [Dog(), Cat()]
for animal in animals:
animal.make_sound()
The four pillars are really split into two parts:
class to code:
Encapsulation and Abstraction are what classes do.
So they are the interaction of classes with the code.
class to class:
Inheritance and Polymorphism are how classes behave with each other.
So they are the interaction of classes with other classes.