OOP concepts in Python
Python is an object-oriented programming language. We can easily create and use classes and objects in Python.
Major object-oriented concepts of Python programming language are given below −
Object
Class
Method
Inheritance
Polymorphism
Data Abstraction
Encapsulation
Object
Object is an entity that has state and behaviour. It may be physical and logical. For example: mouse, keyboard, chair, table, pen etc.
Everything in Python is an object, and almost everything has attributes and methods.
Class
Class can be defined as a collection of objects. It is a logical entity that has some specific attributes and methods. For example: if you have a student class then it should contain an attribute and method i.e. an email id, name, age, roll number etc.
Method
Method is a function that is associated with an object. In Python, method is not unique to class instances. Any object type can have methods.
Inheritance
Inheritance specifies that one object acquires all the properties and behaviors of parent object. By using inheritance we can define a new class with a little or no changes to the existing class. The new class is known as derived class or child class and from which it inherits the properties is called base class or parent class. It provides re-usability of the code.
Polymorphism
Polymorphism defines that one task can be performed in different ways. For example: We have a class animal and all animals talk. But they talk differently. Here, the "talk" behaviour totally depends on the animal. So, the abstract "animal" does not actually "talk", but specific animals have a concrete implementation of the action "talk".
Encapsulation
Encapsulation is used to restrict access to methods and variables. In encapsulation, code and data are wrapped together within a single unit from being modified by accident.
Data Abstraction
Data abstraction and encapsulation are synonymous as data abstraction is achieved through encapsulation.
Abstraction is used to hide internal details and show only functionalities. Abstracting something means to give names to things, so that the name captures the basic idea of what a function or a whole program does.