Understand OOPs Concept in Python
Welcome to the new concept of oops popularly known as object oriented programing in python. Python got it’s unique features which supports multiple concepts. Oops concept is one of them.
Most popular and easiest approach to address an programming challenge is by creating objects, which are nothing but Object-Oriented Programming in python. The object is related to real-word entities such as book, house, pencil, etc.
- It is easy to understand program using oops.
- Oops focuses on writing usable code.
- Data is always safe with data abstraction.
The main concepts of Object-oriented programming (Oops) are:
The class can be defined as a collection of objects. A class contains the blueprints or the prototype from which the objects are being created. It is a logical entity. For example: if you have an student class, then it should contain an attribute and method, i.e. name, age, course, branch etc.
- Classes are created by keyword class.
- The variables that make up a class are called attributes.
- Attributes can be accessed using the dot (.) operator. Eg.: Student.attribute
< statement - 1 >
< statement - N >
Class Example: creating an empty class
The object is an entity that has state and behavior. It may be any real-world object like the mouse, keyboard, chair, table, pen, etc.
An object has two characteristics:
· Attributes:- Data describing the object.
· Behavior:- Behavior methods on the attributes.
Example: creating an object.
obj = Student()
Inheritance is the most important aspect of object-oriented programming, which can create a class which uses all the properties and behavior of another class. The new class that derives properties is called the derived class or child class and the one from which the properties are being inherited is called the base class or parent class. It provides the re-usability of the code.
syntax: syntax of inheritance
Polymorphism simply means having many forms. Polymorphism is an ability (in OOP) to use a common interface for multiple forms (data types). Like other programming languages say Java, C+, polymorphism is also implemented in python for different purpose commonly Duck Typing, Operator overloading and Method overloading, and Method overriding. This polymorphism process can be achieved in two main ways namely overloading and overriding.
output : 9print('4' + '5')
output : 45"print("walk" + "ing")
output : walking
In the above example we can clearly see polymorphism in addition operator.
In the first example the as the data sent are two integer values, the operator did the addition operation of two numbers but, In the second and third example as the data sent are two strings, the operator did the concatenation.
Encapsulation is also an essential aspect of object-oriented programming. It describes the idea of wrapping data and the methods that work on data within one unit. It is used to restrict access to methods and variables. To avoid accidental change, an object’s variable can only be changed by an object’s method. Those types of variables are known as private variables.
Abstraction focuses on hiding the internal implementations of a process or method from the user. In this way, the user knows what he is doing but not how the work is being done. A powerful way to manage abstraction is through the use of hierarchical classification.
After reading, you must have got a good idea of important concepts of object oriented programming used in Python. The main concept of OOPs is to bind the data and the functions that work on that together as a single unit so that no other part of the code can access this data. Your next step should be practice the concepts you have learnt.
Rai Mohit S.