Introduction

Many people learn object-oriented programming in different languages such as C, C++, and Java. Things are pretty simple in C, C++, and Java. There are three magical, easy-to-remember access modifiers (public, protected, private) to get the job done. But Python has no such thing. This can be a little confusing at first, but it's also possible. We'll see how to do it properly in Python. Python has no such mechanism to effectively prevent accessing variables or calling member methods. It's all a matter of culture and convention.

Public

In Python, all member variables and methods are public by default. So, if you want the members to be public, do nothing. Public members (usually methods declared in a class) are accessible outside the class. Objects of the same class must call public methods.

Private

Making a data item private means that no one can access it from outside the class. In other words, you can't touch this policy. Attempting to do so will raise an AttributeError. Private members of a class are accessible only within the class. In Python, private elements can be defined using the __ (double underscore) prefix. To access the private variable we have to prepend _classname (See below example).

Protected

We can say that a protected variable, or a protected member of a class, can only be used by member functions and class members of the same class. It can also be accessed or inherited from its derived classes (child classes). You can change the values ​​of the protected variables of the class. The syntax to follow for protecting variables is to put a single underscore (_) after the variable name.


Access Modifiers

Can access in Same Class?

Can access in Derived Classes?

Can access in Same Packages?

Can access in Other Classes?

Public

YES

YES

YES

YES

Protected

YES

YES

YES

NO

Private

YES

NO

NO

NO


Image1 Image2 Image3 Image4
Inheritance

Inheritance is the mechanism by which a child class receives the parent class's properties. The new class is referred to as a subclass, child class, or derived class, whereas the existing class is referred to as a base class or parent class.

Types of Inheritance

There are five types of inheritance in Python, depending on the number of parent and child classes. Here is a list of different inheritance types:

  1. Single inheritance
  2. Multiple Inheritance
  3. Multilevel inheritance
  4. Hierarchical Inheritance
  5. Hybrid Inheritance
Single inheritance

The simplest form of inheritance is single inheritance, in which a single child class is derived from a single parent class. It is also known as Simple Inheritance due to its straightforward nature.

Image5
Example:
Image6 Image7
Multiple Inheritance

A single child class may inherits from two or more parent classes when there is multiple inheritance. It denotes that all of the parent classes' methods and attributes are accessible to the child class.

Image8
Example:
Image9 Image10
Multilevel inheritance

A class inherits from a child class or derived class under multilevel inheritance. We move beyond just a parent-child relationship when we talk about Multilevel inheritance. We describe grandparents, great-grandparents, grandchildren, etc. We have only seen two levels of inheritance with a superior parent class or classes and a derived class or classes; however, in this case, there may be several levels where the parent class or classes itself are derived from another class or classes.

Image11

Example:
Image12 Image13
Hierarchical Inheritance

Hierarchical inheritance is the inverse of Multiple inheritance. More than one child class is derived from a single parent class in hierarchical inheritance. In other words, there is one parent class and several child classes.

Image14
Example:
Image15 Image16
Hybrid Inheritance

The combination of two or more types of inheritance is known as hybrid inheritance. We can have many relationships between parent and child classes at various levels here.

Image17
Example:
Image18 Image19
Python super() function and benefits

We can refer to the parent class in a child class by using the super() function. The super function returns a temporary parent class object, which allows us to call a parent class method from within a child class method.

Benefits:
  1. The super() method can be used in both single and multiple inheritances.
  2. To access the methods of the parent class, we do not need to remember or specify its name.
  3. Because there is no need to write the entire function, the super() function promotes code reuse.
Image20
Method Overriding in Inheritance

Method overriding is an object-oriented programming language feature that allows a subclass or child class to provide a specific implementation of a method already provided by one of its super-classes or parent classes. This disparity is caused by the methods' similar naming convention.
We can see a situation where the parent class's init() is overridden by the child class's init(), resulting in the child class being unable to inherit attributes from the parent class.

Image21

Similar issues might arise when methods other than init() share the same naming pattern between parent and child classes. To solve this problem without having to type out the parent name, use super(). It refers to the parent class from which the child class is derived automatically.

Image22
Encapsulation

Encapsulation is a technique for combining the code operating on the methods and the variables into a single entity. Encapsulation means that a class's variables are kept private from other classes and are only accessible through its own methods. Thus, creating a class indicates that we are implementing encapsulation. A class is a good illustration of encapsulation since it unifies all the methods and data members (instance variables) into a single entity.

Example:
Image23 Image24 Image25
Polymorphism

In Python, polymorphism refers to an object's ability to assume several forms. Simply said, polymorphism enables us to carry out a single activity in a variety of ways. Let us better understand polymorphism and its implementation through a simple example.

Image26

The code above is the most basic example. The "+" operator is being overloaded over here. You're probably wondering what "overloaded" means. The concept of operator overloading refers to the use of an operator beyond its intended purpose. In our above example, the "+" operator performs addition in the ln [74] , while string concatenation is demonstrated in the ln [76] . The output would demonstrate the distinction. You can see how the same operator was used in two different ways in this example. As a result, operator overloading can be considered an implementation of Polymorphism.

Keep Learning!

Share this post