Advertisement
GamerBhai02

APP Exp 13

May 22nd, 2025
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.57 KB | Source Code | 0 0
  1. class Student:
  2.     college_name = "NHCE"
  3.     def __init__(self, name, age):
  4.         self.name = name
  5.         self.age = age
  6.     # Instance Method
  7.     def display(self):
  8.         print(f"Student name : {self.name}, Age : {self.age}")
  9.     @classmethod
  10.     def change_college(cls, new_name):
  11.         cls.college_name = new_name
  12.     @staticmethod
  13.     def welcome_message():
  14.         print("Welcome to the Student Management System")
  15.  
  16. # Inheritance in Python
  17. class Person:
  18.     def __init__(self, name, age):
  19.         self.name = name
  20.         self.age = age
  21.     def show_details(self):
  22.         print(f"Name: {self.name}, Age: {self.age}")
  23. # Child class inheriting from Person
  24. class Employee(Person):
  25.     def __init__(self, name, age, salary):
  26.         super().__init__(name, age)
  27.         self.salary = salary
  28.     def show_details(self):
  29.         super().show_details()  # Calling parent method
  30.         print(f"Salary: {self.salary}")
  31.  
  32. class Cat:
  33.     def sound(self):
  34.         return "Meow"
  35. class Dog:
  36.     def sound(self):
  37.         return "Bark"
  38. # Function demonstrating polymorphism
  39. def make_sound(animal):
  40.     print(animal.sound())
  41.  
  42. # Demonstrating Types of Methods
  43. s1 = Student("Alice", 20)
  44. s1.display()  # Instance Method
  45. Student.welcome_message()  # Static Method
  46. Student.change_college("New Horizon College Of Engineering")  # Class method
  47. print("Updated College Name:", Student.college_name)
  48.  
  49. # Demonstrating Inheritance
  50. emp1 = Employee("John", 30, 50000)
  51. emp1.show_details()
  52.  
  53. # Demonstrating Polymorphism
  54. cat = Cat()
  55. dog = Dog()
  56. make_sound(cat)
  57. make_sound(dog)
Tags: exp 13
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement