Skip to content
Β 
Β 

Repository files navigation

🐍 Python OOP: Abstract Class & Method Example

🎯 AIM

To create an abstract class named Shape with an abstract method calculate_area, and implement this method in two subclasses: Rectangle and Circle.


🧠 ALGORITHM

  1. Import ABC module:

    • Use from abc import ABC, abstractmethod to define abstract classes and methods.
  2. Create Abstract Class Shape:

    • Define an abstract method calculate_area() with @abstractmethod.
  3. Create Subclass Rectangle:

    • Set default values for length and breadth.
    • Override calculate_area() to compute the rectangle area.
  4. Create Subclass Circle:

    • Set default value for radius.
    • Override calculate_area() to compute the circle area.
  5. Create Objects & Call Methods:

    • Instantiate Rectangle and Circle.
    • Call their calculate_area() methods.

πŸ’» Program

from abc import ABC, abstractmethod
import math
class Shape(ABC):
    @abstractmethod
    def calculate_area(self):
        pass
class Rectangle(Shape):
    def __init__(self, length=1, breadth=1):
        self.length = length
        self.breadth = breadth

    def calculate_area(self):
        return self.length * self.breadth
class Circle(Shape):
    def __init__(self, radius=1):
        self.radius = radius

    def calculate_area(self):
        return math.pi * self.radius *self radius
rect = Rectangle(5, 3)
circle = Circle(4)

print("Area of Rectangle:", rect.calculate_area())
print("Area of Circle:", circle.calculate_area())

Output

WhatsApp Image 2025-10-20 at 19 15 32_e3f8677e

Result

Thus , the program has been executed successfully.

🐍 Python OOP: Encapsulation with Private Members

🎯 AIM

To implement Encapsulation in Python by defining a class Rectangle with private member variables __length and __breadth.


🧠 ALGORITHM

  1. Define the Class:

    • Create a class Rectangle with two private attributes: __length and __breadth.
  2. Initialize Variables:

    • Use the __init__() constructor to set initial values for __length and __breadth.
  3. Print Values:

    • Display the private variables from within the class to demonstrate access.
  4. Instantiate the Object:

    • Create an object of the Rectangle class to trigger the constructor.

πŸ’» Program

class Rectangle:
    def __init__(self, length, width):
        # private variables
        self.__length = length
        self.__width = width

    def display(self):
        # accessing private variables within the class
        print(self.__length)
        print(self.__width)

# create object
rect = Rectangle(5, 3)

# print values within the class
rect.display()

Output

Screenshot 2025-10-20 191840

Result

Thus , the program has been executed successfully.

🐟 Method Overriding-Fish and Shark Class Inheritance in Python

🧠 AIM:

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.

πŸ“‹ ALGORITHM:

  1. Define the Fish class with a method named type() that prints "fish".
  2. Define the Shark class as a subclass of Fish, and override the type() method to print "shark".
  3. Create an instance of the Fish class named obj_goldfish.
  4. Create an instance of the Shark class named obj_hammerhead.
  5. Use a for loop to iterate over both objects.
  6. Within the loop, call the type() method using the loop variable.
  7. Output will demonstrate method overriding: printing "fish" and "shark" accordingly.

πŸ’» PROGRAM:

class Fish:
    def type(self):
        print("fish")
class Shark:
    def type(self):
        print("shark")
obj_goldfish=Fish()
obj_hammerhead=Shark()

for i in (obj_goldfish,obj_hammerhead):
    i.type()

OUTPUT

Screenshot 2025-10-20 193207

RESULT

Thus , the program has been executed successfully.

🐍 Python OOP: Operator Overloading (Less Than <)

🎯 AIM

To write a Python program that demonstrates operator overloading by overloading the less than (<) operator using a custom class.


🧠 ALGORITHM

  1. Create Class A:

    • Define the __init__() method to initialize the object with a value a.
  2. 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"
  3. Create Objects:

    • Instantiate two objects ob1 and ob2 with values.
  4. Use < Operator:

    • Use print(ob1 < ob2) to trigger the overloaded behavior.

πŸ’» Program

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(20)
ob2 = A(3)
print(ob1 < ob2)

Output

Screenshot 2025-10-20 192450

Result

Thus , the program has been executed successfully.

# 🐍 Python OOP: Polymorphism with Classes

🎯 AIM

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.


🧠 ALGORITHM

  1. Create Class Beans:

    • Define type() method that prints "Vegetable".
    • Define color() method that prints "Green".
  2. Create Class Mango:

    • Define type() method that prints "Fruit".
    • Define color() method that prints "Yellow".
  3. Define Generic Function func(obj):

    • Call obj.type() and obj.color() β€” this works with both Beans and Mango objects, showcasing polymorphism.
  4. Create Objects:

    • Instantiate Beans and Mango.
    • Pass them to func() and execute the program.

πŸ’» Program

class Beans(): 
     def type(self): 
       print("Vegetable") 
     def color(self):
       print("Green") 
class Mango(): 
     def type(self): 
       print("Fruit") 
     def color(self): 
       print("Yellow")      

obj_beans = Beans() 
obj_mango = Mango() 
for i in (obj_beans,obj_mango):
    i.type()
    i.color()
    

Output

Screenshot 2025-10-20 192814

Result

Thus , the program has been executed successfully.

About

No description, website, or topics provided.

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors