You are on page 1of 13

UNIT-V

CLASSES IN PYTHON
In Python, object-oriented Programming (OOPs) is a programming paradigm that uses
objects and classes in programming. It aims to implement real-world entities like
inheritance, polymorphisms, encapsulation, etc. in the programming. 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.
Main Concepts of Object-Oriented Programming (OOPs)
 Class
 Objects
 Polymorphism
 Encapsulation
 Inheritance
 Data Abstraction

Class
The class can be defined as a collection of objects. It is a logical entity that has some specific
attributes and methods. For example: if you have an employee class, then it should contain
an attribute and method, i.e. an email id, name, age, salary, etc.
Syntax
class ClassName:
<statement-1>
. .
<statement-N>

Object
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.
Everything in Python is an object, and almost everything has attributes and methods. All
functions have a built-in attribute __doc__, which returns the docstring defined in the
function source code.
When we define a class, it needs to create an object to allocate the memory. Consider the
following example.
Example:
1. class car:
2. def __init__(self,modelname, year):
3. self.modelname = modelname
4. self.year = year
5. def display(self):
6. print(self.modelname,self.year)
7.
8. c1 = car("Toyota", 2016)
9. c1.display()
Output:
Toyota 2016
In the above example, we have created the class named car, and it has two attributes model
name and year. We have created a c1 object to access the class attribute. The c1 object will
allocate memory for these values. We will learn more about class and object in the next
tutorial.
Method
The method is a function that is associated with an object. In Python, a method is not unique
to class instances. Any object type can have methods.
Inheritance
Inheritance is the most important aspect of object-oriented programming, which simulates
the real-world concept of inheritance. It specifies that the child object acquires all the
properties and behaviors of the parent object.
By using inheritance, we can create a class which uses all the properties and behavior of
another class. The new class is known as a derived class or child class, and the one whose
properties are acquired is known as a base class or parent class.
It provides the re-usability of the code.
Polymorphism
Polymorphism contains two words "poly" and "morphs". Poly means many, and morph
means shape. By polymorphism, we understand that one task can be performed in different
ways. For example - you have a class animal, and all animals speak. But they speak
differently. Here, the "speak" behavior is polymorphic in a sense and depends on the animal.
So, the abstract "animal" concept does not actually "speak", but specific animals (like dogs
and cats) have a concrete implementation of the action "speak".
Encapsulation
Encapsulation is also an essential aspect of object-oriented programming. It is used to restrict
access to methods and variables. In encapsulation, code and data are wrapped together within
a single unit from being modified by accident.
Data Abstraction
Data abstraction and encapsulation both are often used as synonyms. Both are nearly
synonyms because data abstraction is achieved through encapsulation.
Abstraction is used to hide internal details and show only functionalities. Abstracting
something means to give names to things so that the name captures the core of what a
function or a whole program does.
Inheritance
The method of inheriting the properties of parent class into a child class is known as inheritance. It is an
OOP concept. Following are the benefits of inheritance.
1. Code reusability- we do not have to write the same code again and again, we can just inherit the
properties we need in a child class.
2. It represents a real world relationship between parent class and child class.
3. It is transitive in nature. If a child class inherits properties from a parent class, then all other sub-
classes of the child class will also inherit the properties of the parent class.
Below is a simple example of inheritance in python.
class Parent():
def first(self):
print('first function')

class Child(Parent):
def second(self):
print('second function')

ob = Child()
ob.first()
ob.second()

Output:
first function
second function
In the above program, you can access the parent class function using the child class object.
Sub-classing
Calling a constructor of the parent class by mentioning the parent class name in the declaration of the child
class is known as sub-classing. A child class identifies its parent class by sub-classing.
__init__( ) Function
The __init__() function is called every time a class is being used to make an object. When we add the
__init__() function in a parent class, the child class will no longer be able to inherit the parent class’s
__init__() function. The child’s class __init__() function overrides the parent class’s __init__() function.

class Parent:
def __init__(self , fname, fage):
self.firstname = fname
self.age = fage
def view(self):
print(self.firstname , self.age)
class Child(Parent):
def __init__(self , fname , fage):
Parent.__init__(self, fname, fage)
self.lastname = "edureka"
def view(self):
print("course name" , self.firstname ,"first came", self.age , " years ago." , self.lastname, " has
courses to master python")
ob = Child("Python" , '28')
ob.view()

Types Of Inheritance
Depending upon the number of child and parent classes involved, there are four types of inheritance in
python.

Single Inheritance:
Single inheritance enables a derived class to inherit properties from a single parent class, thus enabling
code reusability and the addition of new features to existing code.
EXAMPLE:

class Parent:
def func1(self):
print("this is function one")
class Child(Parent):
def func2(self):
print(" this is function 2 ")
ob = Child()
ob.func1()
ob.func2()

Multiple Inheritance:
When a class can be derived from more than one base class this type of inheritance is called multiple
inheritances. In multiple inheritances, all the features of the base classes are inherited into the derived
class.

EXAMPLE
class Parent:
def func1(self):
print("this is function 1")
class Parent2:
def func2(self):
print("this is function 2")
class Child(Parent , Parent2):
def func3(self):
print("this is function 3")
ob = Child()
ob.func1()
ob.func2()
ob.func3()

Multilevel Inheritance : When a child class inherits from more than one parent class.
In multilevel inheritance, features of the base class and the derived class are further inherited into the new
derived class. This is similar to a relationship representing a child and a grandfather.

EXAMPLE

class Parent:
def func1(self):
print("this is function 1")
class Child(Parent):
def func2(self):
print("this is function 2")
class Child2(Child):
def func3(self)
print("this is function 3")
ob = Child2()
ob.func1()
ob.func2()
ob.func3()

Hierarchical Inheritance: Hierarchical inheritance involves multiple inheritance from the same base or
parent class.

When more than one derived class are created from a single base this type of inheritance is called
hierarchical inheritance. In this program, we have a parent (base) class and two child (derived) classes.
EXAMPLE:
class Parent:
def func1(self):
print("this is function 1")
class Child(Parent):
def func2(self):
print("this is function 2")
class Child2(Parent):
def func3(self):
print("this is function 3")

ob = Child()
ob1 = Child2()
ob.func1()
ob.func2()

DATABASE CONNECTIVITY
Connecting MySQL with Python

To create a connection between the MySQL database and Python, the connect() method
of mysql.connector module is used. We pass the database details like HostName,
username, and the password in the method call, and then the method returns the connection
object.

The following steps are required to connect SQL with Python:

Step 1: Download and Install the free MySQL database from here.

Step 2: After installing the MySQL database, open your Command prompt.

Step 3: Navigate your Command prompt to the location of PIP.

pip install package_name

Click here to see, How to install PIP?

Step 4: Now run the commands given below to download and install “MySQL Connector”.
Here, mysql.connector statement will help you to communicate with the MySQL database.

Download and install “MySQL Connector”

pip install mysql-connector-python

Step 5: Test MySQL Connector

To check if the installation was successful, or if you already installed “MySQL


Connector”, go to your IDE and run the given below code :

import mysql.connector

If the above code gets executed with no errors, “MySQL Connector” is ready to be used.

Step 6: Create Connection

Now to connect SQL with Python, run the code given below in your IDE.

Python3
# Importing module

import mysql.connector

# Creating connection object

mydb = mysql.connector.connect(

host = "localhost",

user = "yourusername",

password = "your_password"

# Printing the connection object

print(mydb)

Output:

You might also like