There are five important features related to Object Oriented Programming System.They are:
The entire OOPS methodology has been derived from a single root concept called 'object'.
An object is anything that really exists in the world and can be distinguished from others.
This definition specifies that everything in this world is an object. For example, a table, a ball, a car, a dog, a person, etc. will come under objects.
A class is a user defined blueprint or prototype from which objects are created
Let's create a class with the name Person for which Raju and Sita are objects. A class is created by using the keyword,class. A class describes the attributes and actions performed by its objects. So, we write the attributes (variables) and actions (functions) in the class as:
Person class has two variables and one function. The function that is written in the class is called method. When we want to use this class, we should create an object to the class as:
p1 = Person()
Here, p1 is an object of Person class. Object represents memory to store the actual data. The memory needed to create p1 object is provided by PVM.
Observe the function (or method) in the class:
Here, 'cls' represents a default parameter that indicates the class. So, cls.name refers to class variable 'Raju'. We can call the talk() method to display Raju's details as:
Encapsulation is a mechanism where the data (variables) and the code (methods) that act on the data will bind together. For example, if we take a class, we write the variables and methods inside the class. Thus, class is binding them together. So class is an example for encapsulation.
For example, we can write a Student class with 'id' and 'name' as attributes along with the display() method that displays this data. This Student class becomes an example for encapsulation.
There may be a lot of data, a class contains and the user does not need the entire data. The user requires only some part of the available data. In this case, we can hide the unnecessary data from the user and expose only that data that is of interest to the user. This is called abstraction.
In languages like Java, we have keywords like private, protected and public to implement various levels of abstraction. These keywords are called access specifiers. In Python, such words are not available. Everything written in the class will come under public. That means everything written in the class is available outside the class to other people. Suppose, we do not want to make a variable available outside the class or to other members inside the class, we can write the variable with two double scores before it as: __var. This is like a private variable in Python. In the following example, 'y' is a private variable since it is written as: __y.
Now, it is not possible to access the variable from within the class or out of the class as:
m = Myclass()
The preceding print() statement displays error message as: AttributeError: 'Myclass' object has no attribute 'y'. Even though, we cannot access the private variable in this way, it is possible to access it in the format:
That means we are using Classname differently to access the private variable. This is called name mangling. In name mangling, we have to use one underscore before the classname and two underscores after the classname. Like this, using the names differently to access the private variables is called name mangling.
Creating new classes from existing classes, so that the new classes will acquire all the features of the existing classes is called Inheritance. A good example for Inheritance in nature is parents producing the children and children inheriting the qualities of the parents.
The word 'Polymorphism' came from two Greek words 'poly' meaning 'many' and 'morphos' meaning 'forms'. Thus, polymorphism represents the ability to assume several different forms. In programming, if an object or method is exhibiting different behavior in different contexts, it is called polymorphic nature. Polymorphism provides flexibility in writing programs in such a way that the programmer uses same method call to perform different operations depending on the requirement.
When a function can perform different tasks, we can say that it is exhibiting polymorphism.
The programming languages which follow all the five features of OOPS are called object oriented programming languages. For example, C++, Java and >Python will come into this category.