Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Student:
- college_name = "NHCE"
- def __init__(self, name, age):
- self.name = name
- self.age = age
- # Instance Method
- def display(self):
- print(f"Student name : {self.name}, Age : {self.age}")
- @classmethod
- def change_college(cls, new_name):
- cls.college_name = new_name
- @staticmethod
- def welcome_message():
- print("Welcome to the Student Management System")
- # Inheritance in Python
- class Person:
- def __init__(self, name, age):
- self.name = name
- self.age = age
- def show_details(self):
- print(f"Name: {self.name}, Age: {self.age}")
- # Child class inheriting from Person
- class Employee(Person):
- def __init__(self, name, age, salary):
- super().__init__(name, age)
- self.salary = salary
- def show_details(self):
- super().show_details() # Calling parent method
- print(f"Salary: {self.salary}")
- class Cat:
- def sound(self):
- return "Meow"
- class Dog:
- def sound(self):
- return "Bark"
- # Function demonstrating polymorphism
- def make_sound(animal):
- print(animal.sound())
- # Demonstrating Types of Methods
- s1 = Student("Alice", 20)
- s1.display() # Instance Method
- Student.welcome_message() # Static Method
- Student.change_college("New Horizon College Of Engineering") # Class method
- print("Updated College Name:", Student.college_name)
- # Demonstrating Inheritance
- emp1 = Employee("John", 30, 50000)
- emp1.show_details()
- # Demonstrating Polymorphism
- cat = Cat()
- dog = Dog()
- make_sound(cat)
- make_sound(dog)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement