By Sadra via Discussions on Python.org at 01Jun2022 05:21:
Can anyone help me with this scenario…Im a newbie!
I want to write a code in Python using Inheritance method. Hypothetically there are 3 different universities (Class A, B, C, D). Each of these Uni’s have different students in dif courses e.g regular students in MA courses; scholarship student in BA and…
There is specific features for Class A university students like name,
address, students info etc and specific features for other uni’s
I think we need some more detail.
It isn’t clear to me whether you need inheritance at all. Do the various
univeristies have different behaviours, or just some record keeping
details? Normally a subclass inheriting from a parent “superclass”
implies that the subclass is a different type of thing, for example a
college, a TAFE, a trade school etc, where each does a different kind of
teaching or has a different way of doing things. As opposed to 3
different universities which all behave the same way but have different
specific details. Where you draw that line may be a bit subjective, of
Inheritance itself is easy enough:
''' Common behaviours for all univeristies.
def __init__(self, name):
# they all have a name
self.name = name
''' A special type A university.
def __init__(self, name, something_else):
super().__init__(name) # common initialisation
# this is specific to type "A" universities
self.something_else = something_else
and a similar split up of methods when you come to that.
__init__ method for
UniA calls the common
__init__ if the
University from which it inherits. Then does some
additional work specific to the
UniA class, in this case just saving
an additional attribute. So you might create a new type A university
uni1 = UniA("Uni A Name", "specialty")
University objects, is will have a
.name attribute, and
UniA objects it will also have a
That is the general structure for a subclass (
UniA) inheriting from a
University). You can follow the same approach for other
methods in the class. Common methods aree writtin in the superclass, and
if they are different an overriding method of the same name can be
written in a subclass to do something specific.
There are several tutorials on classes and inheritance found by this
I have no opinion about which are better or worse.
I had a look at the tutorial in the main python docs, but it is written
for people already familiar with programming and feels too techical for
Cameron Simpson email@example.com