Python object oriented programming is a paradigm. It always uses objects and classes in programming. It mainly specifies how to implement real world entities like inheritance, polymorphisms, encapsulation etc. This programming concept of OOPs is combining data and also the functions that work on that together as a single unit, so that no other part of code will access this data. There are main concepts of OOP are:
- Class
- Objects
- Polymorphism
- Encapsulation
- Inheritance
- Data Abstraction
Class
A class will be a collection of objects. A class will contain the blueprints or the prototype from which the objects that are being created. It will be a logical entity which contains some of the attributes and methods.
If we have to understand the need for creating a class, for example, we want to track the number of dogs that may have different attributes like breed, age. It has a list that is used, the first element that could represent age.
There are some points on python class
- classes that will be created by keyword class
- Attributes are variables that are included in a class.
- Attributes are always public and can be accessed by using a dot(.) operator.
Class Definition Syntax is
class ClassName:
# Statement-1
.
.
.
# Statement-N
Objects
The object is considered as an entity that has a state and behavior associated with it. It will also be any real world object such as a mouse, keyboard, chair, table and pen. Integers,strings,floating-point numbers, even arrays and also dictionaries will all be objects. An object consists of
- State: It shows by the attributes of an object. It has the properties of an object.
- Behavior: It is represented by the methods of the object. It also shows the response of an object to other objects.
- Identity- It gives a unique name to an object and enables one object to interact with other objects.
The self
- Class methods will have an extra first parameter in the method definition. When we do not give the value for this parameter when we call the method, Python provides it.
- If we have a method that takes no arguments,then we still have to have one argument.
- This is similar to this pointer in C++ and this reference will be in Java.
The _Init_ Method:
The init_Method will be similar to construction in C++ and Java. It runs as soon as the object of a class is instantiated. This method is useful to do any initialisation we want to do with our object.
Inheritance
Inheritance will be a capability of one of my classes that derives or inherits the properties from another class. The class that derives properties is called the derived class or child class from which the properties that are being derived are called the base class or parent class. The
Benefits of inheritance are:
It shows real world relationships well.
It gives the reusability of a code. We need not write the similar code repeatedly
It will be transitive in nature, that means if class A inherits from another class B then all other subclasses will inherit from class B.
Polymorphism:
Polymorphism means having many forms. For example, we need to say if a species of birds fly or not by using polymorphism we may do this by using a single function.
Encapsulation:
Encapsulation is one of the basic concepts in object oriented programming. It describes the main idea of wrapping data and the methods which work on data within one unit. This puts restrictions on accessing variables and methods directly and can prevent the change of an object’s variable that can only be changed by an object’s method. These types of variables are known as private variables.
Data Abstraction
It will hide the unnecessary code details from the user. When we do not want to give up the sensitive parts of code, data abstraction can be used.
Questions
- What is Python Oops?
- What are different concepts of Oops?
One Response