DSP Practical 6,7 (IT)
DSP Practical 6,7 (IT)
Class myclass:
def instances_method(self):
@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
class Animal(ABC):
@abstractmethod
def sound(self):
pass
@staticmethod
def general_info():
def info(self):
print(f"This is a {self.__class__.__name__}.")
class Dog(Animal):
def sound(self):
print("Dog barks")
class Cat(Animal):
def sound(self):
print("Cat meows")
# Main execution
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):
class Dog(Animal):
def sound(self):
print("Bark")
class Cat(Animal):
def sound(self):
print("Meow")
# Child class 3 inheriting from Animal
class Cow(Animal):
def sound(self):
print("Moo")
def make_animal_sound(animal):
animal.sound()
dog = Dog()
cat = Cat()
cow = Cow()
# Calling the same method, but it behaves differently depending on the object