-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathInheritance_Implementation.py
More file actions
129 lines (112 loc) · 4.19 KB
/
Inheritance_Implementation.py
File metadata and controls
129 lines (112 loc) · 4.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/usr/bin/python3
# -----------------------------------------------------------------------------
# Script Name: Inheritance_Implementation.py
# Experiment: Experiment 4, Program 4.1
# Description: Demonstrates Inheritance in Python (Single Inheritance).
# The SECO class inherits properties and methods from the Student class.
#
# Authors: Amey Thakur
# Repository: https://github.com/Amey-Thakur/OPEN-SOURCE-TECH-LAB
# License: CC BY 4.0
# -----------------------------------------------------------------------------
# -----------------------------------------------------------------------------
# PARENT CLASS (BASE CLASS)
# -----------------------------------------------------------------------------
class Student:
"""
Base Class representing a generic Student.
"""
no_of_courses = 5
credits = 25
def __init__(self, *args):
"""
Constructor with variable arguments (*args) to simulate Method Overloading.
Can initialize empty, from another object, or with specific values.
"""
if len(args) == 0:
# Default initialization
self.rollno = None
self.name = None
self.addr = None
self.mob = None
elif isinstance(args[0], Student):
# Copy Constructor logic (initializing from another Student object)
self.rollno = args[0].rollno
self.name = args[0].name
self.addr = args[0].addr
self.mob = args[0].mob
elif len(args) == 4:
# Detailed initialization
self.setval(args[0], args[1], args[2], args[3])
else:
print("Error: Invalid arguments passed to Student Constructor.")
def setval(self, rollno, name, addr, mob):
"""
Method to set student details.
"""
self.rollno = rollno
self.name = name
self.addr = addr
self.mob = mob
def getval(self):
"""
Method to print student details.
"""
print(f'RollNo: {self.rollno}')
print(f'Name: {self.name}')
print(f'No. of Courses:{self.no_of_courses}')
print(f'Credits: {self.credits}')
print(f'Mobile: {self.mob}')
# -----------------------------------------------------------------------------
# CHILD CLASS (DERIVED CLASS)
# -----------------------------------------------------------------------------
class SECO(Student):
"""
Derived Class inherited from Student.
Represents a specific category of student (e.g., SE Computer Engineering).
"""
courses = list()
skills = list()
def __init__(self):
"""
Constructor for Child Class.
Invokes Parent Class Constructor.
Includes basic Error Handling demonstration.
"""
try:
# Invoking Parent Class Constructor
super().__init__()
except Exception as e:
print(f'Initialization Error: {e}')
def setprop(self, course, skill):
"""
Method to add courses and desired skills.
Specific to SECO students.
"""
self.courses.append(course)
self.skills.append(skill)
def getprop(self):
"""
Method to retrieve all properties.
Calls parent method getval() to print basic details, then prints specific details.
"""
print("\n--- Student Details (Inherited) ---")
self.getval() # Calling method from Parent Class
print("--- Additional Properties (Child Class) ---")
print(f'Courses: {self.courses}')
print(f'Skills: {self.skills}')
# -----------------------------------------------------------------------------
# DRIVER CODE
# -----------------------------------------------------------------------------
print("\n--- Inheritance Demonstration ---")
# Creating an object of the Child Class (SECO)
s = SECO()
# Using Parent Class method to set values
s.setval('58', 'Mega', 'Mumbai', 9167078027)
# Using Child Class method to set specific properties
s.setprop('OSTL', 'Python')
s.setprop('AOA', 'Design Algos')
s.setprop('CG', 'C Programming')
# Displaying all properties (Child + Parent)
s.getprop()
print("-" * 30)