0% found this document useful (0 votes)
19 views

Inht - Oop - Ipynb - Colaboratory

The document discusses object-oriented programming concepts like inheritance, polymorphism, method overloading and overriding through multiple Python code examples. Some key examples include defining classes like Animal, Dog and Cat with inheritance, calculating areas of geometric shapes like circles and rectangles, and demonstrating polymorphism by drawing different shape objects.

Uploaded by

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

Inht - Oop - Ipynb - Colaboratory

The document discusses object-oriented programming concepts like inheritance, polymorphism, method overloading and overriding through multiple Python code examples. Some key examples include defining classes like Animal, Dog and Cat with inheritance, calculating areas of geometric shapes like circles and rectangles, and demonstrating polymorphism by drawing different shape objects.

Uploaded by

swinef5
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

1 class Animal:

2 def noise(self):
3 pass
4 class Dog(Animal):
5 def noise(self):
6 print('bark')
7
8 class Cat(Animal):
9 def noise(self):
10 print('meouw')
11 obj=Dog()
12 obj.noise()
13 obj1=Cat()
14 obj1.noise()
15
16

bark
meouw

1 class shape:
2 def area(self):
3 pass
4 class Rectangle(shape):
5 def __init__(self,width,height):
6 self.width=width
7 self.height=height
8 def area(self):
9 return self.width*self.height
10 class Circle(shape):
11 def __init__(self,radius):
12 self.radius=radius
13 def area(self):
14 return 3.14159 * self.radius * self.radius
15
16 rectangle = Rectangle(4, 5)
17 print("Rectangle Area:", rectangle.area()) # Output: Rectangle Area: 20
18
19 circle = Circle(3)
20 print("Circle Area:", circle.area()) # Output: Circle Area: 28.27431

Rectangle Area: 20
Circle Area: 28.274309999999996

1 class person:
2 def __init__(self,name,age):
3 self.name=name
4 self.age=age
5 def check_name(self):
6 print(f'Your name and age is {self.name} {self.age}')
7 class student(person):
8 def check_name(self):
9 print(f'The name of student is {self.name} and age is{self.age}')
10 class teacher(student):
11 def check_name(self):
12 print(f'The name of teacher is {self.name} and age is {self.age}')
13 obj1=teacher('hannah',29)
14 obj1.check_name()

The name of teacher is hannah and age is 29

1 #multilevelinheritance
2 class person:
3 def __init__(self,name,age):
4 self.name=name
5 self.age=age
6 def check_name(self):
7 return f"{self.name}, {self.age} years old"
8 class student(person):
9 def __init__(self,name,age,grade):
10 super().__init__(name,age)
11 self.grade=grade
12 def check_name(self):
13 return f'{super().check_name()} grade {self.grade}'
14 class teacher(student):
15 def __init__(self,name,age,grade,subject):
16 super().__init__(name,age,grade)
17 self.subject=subject
18 def check_name(self):
19 return f'{super().check_name()}, teaches {self.subject}'
20 obj = teacher("Alice", 30, 12, "Math")
21 obj.check_name()
22

'Alice, 30 years old grade 12, teaches Math'

1 #polymorphism
2 class Shape:
3 def draw(self):
4 pass
5
6 class Circle(Shape):
7 def draw(self):
8 print("Drawing a circle")
9
10 class Square(Shape):
11 def draw(self):
12 print("Drawing a square")
13
14 # Usage
15 shapes = [Circle(), Square()]
16
17 for shape in shapes:
18 shape.draw()
19

Drawing a circle
Drawing a square

1 #abstract class method


2 import abc
3 class shape:
4 @abc.abstractmethod
5 def area(self):
6 pass
7 class Rectangle(shape):
8 def __init__(self,width,height):
9 self.width=width
10 self.height=height
11 def area(self):
12 return self.width*self.height
13 class Circle(shape):
14 def __init__(self,radius):
15 self.radius=radius
16 def area(self):
17 return 3.14159 * self.radius * self.radius
18 circle = Circle(5) # Create a circle object with radius 5
19 print(f"The area of the circle is {circle.area()}") # Print the area of the circle

The area of the circle is 78.53975

1 class demo1:
2 def d1(self):
3 print("Base Class")
4
5 class demo2(demo1):
6 def d1(self):
7 print("Derived Class")
8 super().d1()
9
10 d = demo2()
11 d.d1()
12

Derived Class
Base Class

1 class Father:
2 def showf(self):
3 print("Father Class")
4
5 class Mother:
6 def showm(self):
7 return 'mother class'
8
9
10 class Child(Father, Mother):
11 def __init__(self):
12 super().__init__()
13
14 def show(self):
15 print(f" hi {super().showm()} Child Class") # use f-string to format the output")
16
17
18
19 c = Child()
20 c.show()
21

hi mother class Child Class

1 class person:
2 def __init__(self, name, age):
3 self.name = name
4 self.age = age
5
6 def check_name(self):
7 return f"{self.name}, {self.age} years old"
8
9 class student(person):
10 def __init__(self, name, age, grade):
11 super().__init__(name, age)
12 self.grade = grade
13
14 def check_name(self):
15 return f'{super().check_name()}, grade {self.grade}'
16
17 class teacher(student):
18 def __init__(self, name, age, grade, subject):
19 super().__init__(name, age, grade)
20 self.subject = subject
21
22 def check_name(self):
23 return f'{super().check_name()}, teaches {self.subject}'
24
25 obj = teacher("Alice", 30, 12, "Math")
26 print(obj.check_name())
27

Alice, 30 years old, grade 12, teaches Math

1 class vehicle:
2 def __init__(self,seating_capacity):
3 self.__seating_capacity=seating_capacity
4 def seats(self,seating_capacity):
5 return f'the seating capacity is {self.__seating_capacity}'
6 def fare(self):
7 return 100*self.__seating_capacity
8 class bus(vehicle):
9 def __init__(self,name,seating_capacity):
10 self.__name=name
11 super().__init__(seating_capacity)
12
13 def __show_name(self):
14 return self.__name
15
16 def total_fare(self):
17 base_fare=super().fare()
18 final_fare=base_fare+0.1*base_fare
19 return f'The seats are {super().seats(25)} and the total fare is {final_fare} and bus name {self.__show_name()}'
20
21 obj=bus('bread',25)
22 obj.total_fare()
23 obj._bus__show_name()
24
25

'bread'

1 class Demo:
2 __p=0
3 def show(self):
4 self.__p=self.__p+1
5 return self.__p
6 d=Demo()
7 d.show()

1 #multiple inheritance
2 # Parent class 1
3 class Animal:
4 def __init__(self, name):
5 self.name = name
6
7 def speak(self):
8 pass
9
10 # Parent class 2
11 class Flyable:
12 def __init__(self, name):
13 self.name = name
14 def fly(self):
15 pass
16
17 # Child class inheriting from both Animal and Flyable
18 class Bird(Animal, Flyable):
19 def speak(self):
20 return f"{self.name} chirps."
21
22 # Child class inheriting only from Animal
23 class Dog(Animal):
24 def speak(self):
25 return f"{self.name} barks."
26
27 # Child class inheriting only from Flyable
28 class Airplane(Flyable):
29 def fly(self):
30 return f"{self.name} is flying."
31
32 # Create instances of the classes
33 parrot = Bird("Polly")
34 golden_retriever = Dog("Buddy")
35 boeing_747 = Airplane("Boeing 747")
36
37 # Demonstrate multiple inheritance
38 print(parrot.speak()) # Output: Polly chirps.
39 print(golden_retriever.speak()) # Output: Buddy barks.
40 print(boeing_747.fly()) # Output: Boeing 747 is flying.
41

Polly chirps.
Buddy barks.
Boeing 747 is flying.

1 #method overloading
2 class Calculator:
3 def add(self, a, b=None, c=None):
4 if b is not None and c is not None:
5 return a + b + c
6 elif b is not None:
7 return a + b
8 else:
9 return a
10
11 # Create an instance of the Calculator class
12 calc = Calculator()
13
14 # Call the add method with different numbers of arguments
15 result1 = calc.add(1)
16 result2 = calc.add(1, 2)
17 result3 = calc.add(1, 2, 3)
18
19 # Output the results
20 print(result1) # Output: 1
21 print(result2) # Output: 3
22 print(result3) # Output: 6
23

1
3
6

1 #method overiding
2 class Shape:
3 def area(self):
4 pass
5
6 class Circle(Shape):
7 def __init__(self, radius):
8 self.radius = radius
9
10 def area(self):
11 return 3.14159 * self.radius * self.radius
12
13 class Rectangle(Shape):
14 def __init__(self, width, height):
15 self.width = width
16 self.height = height
17
18 def area(self):
19 return self.width * self.height
20
21 shapes = [Circle(5), Rectangle(4, 6)]
22 for shape in shapes:
23 print(f"Area: {shape.area()}")
24

Area: 78.53975
Area: 24

You might also like