Classes In Python

Detailed Article on Python Classes, plus Encapsulation or Abstraction with Examples
Written by Paayi Tech |10-Apr-2019 | 0 Comments | 599 Views

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.

 

Instance = Class(arguments)

 

 

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:

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
  • Polymorphism
  • Inheritance

 

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:

class FirstClass(object):
    def getName(self, name):
        self.name = name
    def printName(self):
        print(f"The name is: {self.name}")
 
obj1 = FirstClass()
obj1.getName("Pikachu")
obj1.printName()
       

 

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:

class calculator(object):
    def getNumbers(self,a,b):
        self.a = a
        self.b = b
    def add(self):
        print(self.a+self.b)
 
    def sub(self):
        print(self.a-self.b)
 
    def mult(self):
        print(self.a*self.b)
    def div(self):
        print(self.a/self.b)
 
obj = calculator()
obj.getNumbers(10,2)
print ("Addition")
obj.add()
print ("Subtraction")
obj.sub()
print ("Multiplication")
obj.mult()
print ("Division")
obj.div()

 

#Output

Addition

12

Subtraction

8

Multiplication

20

Division

5.0

 





Login/Sign Up

Comments




Related Posts



© Copyright 2019, All Rights Reserved. paayi.com

This site uses cookies. By continuing to use this site or clicking "I Agree", you agree to the use of cookies. Read our cookies policy and privacy statement for more information.