Python OOPs Concepts

Python has been an object-oriented language since it existed. Because of this, creating and using classes and objects are downright easy. This topic will help you in using Python’s object-oriented programming support.

If you do not have any previous experience with object-oriented (OO) programming, you may want to consult an introductory course on it or at least a discussion of some sort so that you have a grasp of the basic concepts.

However, here is a small introduction of Object-Oriented Programming (OOP) to bring you at speed :

Overview of OOP Terminology

Objects

An object is a physical entity, whereas class is a logical definition. An object can be anything like – a student while designing a school’s record registry, or a pen in stationary’s item management program, or a car in the manufacture’s car database program. There are three important characteristics by which it is identified, they are:

1. Identity: Identity refers to some piece of information that can be used to identify the object of a class. It can be the name of the student, the company name of the car, etc.

2. Properties: The attributes of that object are called properties. Like age, gender, DOB for a student; or type of engine, number of gears for a car.

3. Behavior: The behavior of any object is equivalent to the functions that it can perform. In OOP it is possible to assign some functions to objects of a class. Taking the example forward, like a student can read/write, the car can accelerate and so on.

Class

A class is a blueprint where attributes and behavior is defined. For example, if Amit, Sumit, Sachin, you, and me are objects, then Human Being is a class. An object is the fundamental concept of OOP but classes provide an ability to define the similar types of objects.

In class, both data, and the functions that will be operating on that data are bundled as a unit.

For example,
let’s say there is a class named Car, which has some basic data like – name, model, brand, date of manufacture, engine etc, and some functions like turn the engine on, apply brakes, accelerate, change gear, blow horn etc. Now that all the basic features are defined in our class Car, we can create its objects by setting values for properties name, model etc, and the object of the class Car will be able to use the functions defined in it.

Data Hiding

Data hiding helps us to define the privacy of data from the outside world or to be precise, from other classes. The reason behind doing this is to create several levels of accessing an object’s data and prevent it from accidental modification. Also, hiding, or setting up privacy levels, can be done for functions as well.

In OOP, data inside the classes can be defined as public, private or protected.
* Private data or function are the ones that cannot be accessed or seen from outside the class.
* Public data or functions can be accessed from anywhere. * Protected data or functions, more or less act like public but should not be accessed from outside.

Abstraction of Data

Classes use the concept of abstraction. A class encapsulates the relevant data and functions that operate on data by hiding the complex implementation details from the user. The user needs to focus on what a class does rather than how it does.

Encapsulation

Encapsulation is one of the core reasons for the existence of an object. Since the beginning we have been talking about objects, its data, functions, privacy, etc., now it’s time to know-how is all this kept bounded. The answer is encapsulation. Much as it may sound like a capsule, it is pretty much the same. Here we try to encapsulate the data and functions together which belongs to the same class.

 

Inheritance

Inheritance is about defining a set of core properties and functions in one place and then re-using them by inheriting the class in which they are defined.
Python supports Simple, Multiple and Multilevel Inheritance. We will be covering these in detail in inheritance.

Polymorphism

Polymorphism, or Poly + Morph, means “many forms. Precisely, Polymorphism is the property of any function or operator that can behave differently depending upon the input that they are fed with.

Visitor Counter

0105417