Ad

Strange Inheritance During The Override Of A Method

- 1 answer

I'm developing different objects that are inherited from each other.
At one point I noticed that the code of a super method is still executed by the inherited method.
As it can be seen from the sub-method listed below, I commented super() to prevent the superclass code from being executed.
This is the super-class:

class superClass(superClassOfSuperClass):
    def __init__(self, data):
        #[...]

        #unique call of the superMethod
        self.component.addActionListener(self.superMethod)

        #[...]

    def superMethod(self, param):
        print "I'm executed, but I don't have to be executed!"

This is the sub-method:

class subClass(superClass):
    def __init__(self, data, newData):
        superClass.__init__(self, data)
        self.newData = newData

        #[...]

    def superMethod(self, param):
        #super(superClass, self).superMethod(None)
        print 'only I have to be printed!!!'

The code works. But I don't want to see the superMethod print.

Is there a way to prevent superMethod from running and make the subMethod execute only? Because up to now I can see both print outputs.
I hope it's my shortcoming on OOP.

PS: I'm working with Jython 2.7 as you can see from tags.

Ad

Answer

There must be another explanation, for instance if there are more classes involved and there is another class which inherits from superClass, but does not override superMethod.

The following code (I'm using python 3) shows that everything should work just fine:

class Super:
    def __init__(self, listener):
        listener.append(self.superMethod)

    def superMethod(self, param):
        print("I'm executed, but I don't have to be executed!")


class Sub(Super):
    def superMethod(self, param):
        print('Only me!')

methods = []
s = Sub(methods)

for method in methods:
    method(None)

Output:

Only me!
Ad
source: stackoverflow.com
Ad