Object Oriented Programming in Python

Encapsulation and Inheritance are two fundamental concepts in Object-Oriented Programming (OOP) that allow for the creation of robust and maintainable code.

Encapsulation is achieved by using the private and protected access modifiers. In Python, we use a single underscore (_) before the name of a variable or method to make it private, and a double underscore (__) to make it protected. For example:

Inheritance in Python is achieved by using the class keyword, followed by the name of the class that you want to inherit from. For example:

As you can see, the public members of the classes can be accessed and called directly from the instances, while the private and protected members can only be accessed from within the class.

1 Like