To create an abstract class named Shape
with an abstract method calculate_area
, and implement this method in two subclasses: Rectangle
and Circle
.
-
Import ABC module:
- Use
from abc import ABC, abstractmethod
to define abstract classes and methods.
- Use
-
Create Abstract Class
Shape
:- Define an abstract method
calculate_area()
with@abstractmethod
.
- Define an abstract method
-
Create Subclass
Rectangle
:- Set default values for
length
andbreadth
. - Override
calculate_area()
to compute the rectangle area.
- Set default values for
-
Create Subclass
Circle
:- Set default value for
radius
. - Override
calculate_area()
to compute the circle area.
- Set default value for
-
Create Objects & Call Methods:
- Instantiate
Rectangle
andCircle
. - Call their
calculate_area()
methods.
- Instantiate
from abc import ABC, abstractmethod
from math import pi
class Shape(ABC):
@abstractmethod
def calculate_area(self):
pass
class Rectangle(Shape):
def __init__(self, length=5, breadth=3):
self.length = length
self.breadth = breadth
def calculate_area(self):
print("Area of Rectangle:", self.length * self.breadth)
class Circle(Shape):
def __init__(self, radius=4):
self.radius = radius
def calculate_area(self):
print("Area of Circle:", pi * self.radius ** 2)
r = Rectangle()
r.calculate_area()
c = Circle()
c.calculate_area()
Hence the program is executed and the output is verified
To implement Encapsulation in Python by defining a class Rectangle
with private member variables __length
and __breadth
.
-
Define the Class:
- Create a class
Rectangle
with two private attributes:__length
and__breadth
.
- Create a class
-
Initialize Variables:
- Use the
__init__()
constructor to set initial values for__length
and__breadth
.
- Use the
-
Print Values:
- Display the private variables from within the class to demonstrate access.
-
Instantiate the Object:
- Create an object of the
Rectangle
class to trigger the constructor.
- Create an object of the
class Rectangle:
def __init__(self, l, b):
self.__length = l
self.__breadth = b
print("Length:", self.__length)
print("Breadth:", self.__breadth)
r = Rectangle(10, 5)
Hence the program is executed and the output is verified
To write a Python program that demonstrates class inheritance by creating a parent class Fish
with a method type
, and a child class Shark
that overrides the type
method.
- Define the
Fish
class with a method namedtype()
that prints"fish"
. - Define the
Shark
class as a subclass ofFish
, and override thetype()
method to print"shark"
. - Create an instance of the
Fish
class namedobj_goldfish
. - Create an instance of the
Shark
class namedobj_hammerhead
. - Use a
for
loop to iterate over both objects. - Within the loop, call the
type()
method using the loop variable. - Output will demonstrate method overriding: printing
"fish"
and"shark"
accordingly.
class Fish:
def type(self):
print("fish")
class Shark(Fish):
def type(self):
print("shark")
obj_goldfish = Fish()
obj_hammerhead = Shark()
for obj in (obj_goldfish, obj_hammerhead):
obj.type()
Hence the program is executed and the output is verified
To write a Python program that demonstrates operator overloading by overloading the less than (<
) operator using a custom class.
-
Create Class
A
:- Define the
__init__()
method to initialize the object with a valuea
.
- Define the
-
Overload the
<
Operator:- Define the
__lt__()
method with logic:- If
self.a < o.a
, return"ob1 is less than ob2"
- Else, return
"ob2 is less than ob1"
- If
- Define the
-
Create Objects:
- Instantiate two objects
ob1
andob2
with values.
- Instantiate two objects
-
Use
<
Operator:- Use
print(ob1 < ob2)
to trigger the overloaded behavior.
- Use
class A:
def __init__(self, a):
self.a = a
def __lt__(self, other):
if self.a < other.a:
return "ob1 is less than ob2"
else:
return "ob2 is less than ob1"
ob1 = A(10)
ob2 = A(20)
print(ob1 < ob2)
Hence the program is executed and the output is verified
To create two specific classes — Beans
and Mango
. Then, create a generic function that can accept any object and determine its type (Fruit or Vegetable) and color, using polymorphism.
-
Create Class
Beans
:- Define
type()
method that prints"Vegetable"
. - Define
color()
method that prints"Green"
.
- Define
-
Create Class
Mango
:- Define
type()
method that prints"Fruit"
. - Define
color()
method that prints"Yellow"
.
- Define
-
Define Generic Function
func(obj)
:- Call
obj.type()
andobj.color()
— this works with bothBeans
andMango
objects, showcasing polymorphism.
- Call
-
Create Objects:
- Instantiate
Beans
andMango
. - Pass them to
func()
and execute the program.
- Instantiate
class Beans:
def type(self):
print("Vegetable")
def color(self):
print("Green")
class Mango:
def type(self):
print("Fruit")
def color(self):
print("Yellow")
def func(obj):
obj.type()
obj.color()
b = Beans()
m = Mango()
func(b)
func(m)
Hence the program is executed and the output is verified