How to implement Inheritance method for this scenario?

Hi

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

Can anyone help with this Code?

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
course.

Inheritance itself is easy enough:

class University:
    ''' Common behaviours for all univeristies.
    '''
    def __init__(self, name):
        # they all have a name
        self.name = name

class UniA(University):
    ''' 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.

Here the __init__ method for UniA calls the common __init__ if the
superclass 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
like this:

uni1 = UniA("Uni A Name", "specialty")

Like all University objects, is will have a .name attribute, and
like all UniA objects it will also have a .something_else attribute.

That is the general structure for a subclass (UniA) inheriting from a
superclass (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
search:

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
a beginner.

Cheers,
Cameron Simpson cs@cskk.id.au