0% found this document useful (0 votes)
9 views4 pages

DSP Practical 6,7 (IT)

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)
9 views4 pages

DSP Practical 6,7 (IT)

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/ 4

Write a program to implement static and instances method abstract classes and interfaces.

Class myclass:
def instances_method(self):

return “instances method”

@classmethod

def class_method(cls):

return”class method”

@staticmethod

def static_method():
return”static method”

my_obj= myclass()

print(my_obj.instance_method())

code

from abc import ABC, abstractmethod # Importing Abstract Base Class (ABC) module

# Abstract Class (Interface in Python)

class Animal(ABC):

@abstractmethod

def sound(self):

"""Abstract method: must be implemented by subclasses"""

pass

@staticmethod

def general_info():

"""Static method: not dependent on instance variables"""


print("All animals need food and water.")

def info(self):

"""Instance method: uses instance data"""

print(f"This is a {self.__class__.__name__}.")

# Dog class implementing the Animal interface

class Dog(Animal):

def sound(self):

print("Dog barks")

# Cat class implementing the Animal interface

class Cat(Animal):

def sound(self):

print("Cat meows")

# Main execution

Dog.general_info() # Static method can be called using the class name

dog = Dog() # Create an instance of Dog

cat = Cat() # Create an instance of Cat

dog.info() # Instance method

dog.sound() # Polymorphism via abstract method implementation

cat.info() # Instance method

cat.sound() # Polymorphism via abstract method implementation

polymorphism

polymorphism mean ability to take various forms (same object having different form )
print(5+5)

print(“5”+”5”)

Inheritances

When we define a class that inherits all the properties of other class called inheritances.

Syntax

Class father:

#properties

Class son(father):

#properties

Code

# Parent class

class Animal:

def sound(self):

# General sound method, meant to be overridden

print("Some generic animal sound")

# Child class 1 inheriting from Animal

class Dog(Animal):

def sound(self):

# Overriding the sound method

print("Bark")

# Child class 2 inheriting from Animal

class Cat(Animal):

def sound(self):

# Overriding the sound method

print("Meow")
# Child class 3 inheriting from Animal

class Cow(Animal):

def sound(self):

# Overriding the sound method

print("Moo")

# Polymorphism: different animals making different sounds

def make_animal_sound(animal):

animal.sound()

# Creating objects of Dog, Cat, and Cow

dog = Dog()

cat = Cat()

cow = Cow()

# Calling the same method, but it behaves differently depending on the object

make_animal_sound(dog) # Output: Bark

make_animal_sound(cat) # Output: Meow

make_animal_sound(cow) # Output: Moo

You might also like