Classes provide a bundling of the data and functionality in the programming. Creating a new class means that a user is creating a new instance of object, data, and functions and bundling it all together to make a package. Classes provides high reuse ability of the code. Not only we can access classes with the same file where they are defined but also other files which make it more use able. In python classes there are many syntax and semantic changes as compared to C++, C# and java.
Classes come under the hood of object-oriented programming shortly known as OOP. OOP has three fundamental pillars that are class, object and methods. We can define all three of these as follows:
What is a Class?
The class describes about the object within the class. It describes about the data and methods that are bundled.
What is an Object in Class?
An object is an instance of a class; objects have the behaviors of their class. The object is the actual component of programs, while the class specifies how instances are created and how they behave.
Method contain the functionalities that a class can execute on a run time when it is called.
There are three fundamental protocols of OPP and that are:
- Encapsulation or Abstraction
What is Encapsulation:
Encapsulation or abstraction is the process of data hiding. The abstraction is achieved by the process of encapsulation. OOP heavily relies on encapsulation of data so that no data can be accessed outside of the class. In C++ and java there are keywords like public and private data and in python too there are such keywords to make it more reliable. The data is private for outsiders and can only be accessed by the methods of those classes or the class by which it is inherited.
We can describe encapsulation in a program as following:
In the above code we define a class and named it First-class. It is convention to write class name like this (ClassName, MyClass etc). Than we defined a function which take a parameter and set the value and then we again defined a function to print the set value name. Here it is to be noted that I have written self-keyword in every method of class. The self-keyword shows that this is the part of the class and bind the data with method.
To make the instance of the class we first declare class in a variable called obj1. Than by this variable we accessed the getName method and passed the argument and in last accessed printName method to print the value.
Some more example of encapsulation is following: