0% found this document useful (0 votes)
2 views7 pages

Python Programs 2

Uploaded by

ganeshv9098
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views7 pages

Python Programs 2

Uploaded by

ganeshv9098
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

1.

Write a program to demonstrate Class and Object in Python


2. Write a program of sample constructor in Python
3. Write a program of public,private,protected access modifiers
4. Write a program to demonstrate Multilevel Inheritance
5. Write a program to demonstrate Multiple Inheritance
6. Write a program to demonstrate method overloading
7. Write a program to demonstrate method overriding
8. Write a program to demonstrate Multithreading
9. Write a program to demonstrate Abstract Class
10. Write a program to demonstrate Interface
1. CLASS AND OBJECT

Code

class Student:

def __init__(self, name):


[Link] = name

def display(self):
print("Student name is", [Link])

s1 = Student("Yatin")
[Link]()

Output

Student name is Yatin

2. CONSTRUCTOR

Code

class Person:

def __init__(self):
print("Constructor is called")

p = Person()

Output

Constructor is called

3. PUBLIC, PRIVATE, PROTECTED

Code

class Demo:

def __init__(self):
[Link] = "Public"
self._protected = "Protected"
self.__private = "Private"
obj = Demo()

print("Public:", [Link])
print("Protected:", obj._protected)

Accessing private using name mangling


print("Private:", obj._Demo__private)

Output

Public: Public
Protected: Protected
Private: Private

4. MULTILEVEL INHERITANCE

Code

class Grandparent:
def show1(self):
print("Grandparent class")

class Parent(Grandparent):
def show2(self):
print("Parent class")

class Child(Parent):
def show3(self):
print("Child class")

obj = Child()
obj.show1()
obj.show2()
obj.show3()

Output

Grandparent class
Parent class
Child class
5. MULTIPLE INHERITANCE

Code

class Father:
def show1(self):
print("Father class")

class Mother:
def show2(self):
print("Mother class")

class Child(Father, Mother):


def show3(self):
print("Child class")

obj = Child()
obj.show1()
obj.show2()
obj.show3()

Output

Father class
Mother class
Child class

6. METHOD OVERLOADING

Code

class Math:

def add(self, a, b, c = 0):


return a + b + c

obj = Math()

print([Link](2, 3))
print([Link](2, 3, 4))

Output

5
9
7. METHOD OVERRIDING

Code

class Parent:

def show(self):
print("Parent method")

class Child(Parent):

def show(self):
print("Child method")

obj = Child()
[Link]()

Output

Child method

8. MULTITHREADING

Code

import threading

def task():
for i in range(2):
print("Task running")

t1 = [Link](target = task)
t2 = [Link](target = task)

[Link]()
[Link]()

[Link]()
[Link]()

Output
Task running
Task running
Task running
Task running

Note
Order may change because threads run simultaneously

9. ABSTRACT CLASS

Code

from abc import ABC, abstractmethod

class Shape(ABC):

@abstractmethod
def area(self):
pass

class Circle(Shape):

def area(self):
print("Area of circle")

obj = Circle()
[Link]()

Output

Area of circle

10. INTERFACE (using abstract class)

Code

from abc import ABC, abstractmethod

class Animal(ABC):

@abstractmethod
def sound(self):
pass
class Dog(Animal):

def sound(self):
print("Bark")

obj = Dog()
[Link]()

Output

Bark

You might also like