Why call super() in a constructor?

There is an implicit call to super() with no arguments for all classes that have a parent – which is every user defined class in Java – so calling it explicitly is usually not required. However, you may use the call to super() with arguments if the parent’s constructor takes parameters, and you wish to … Read more

Getting the name of a sub-class from within a super-class

Don’t make the method static. The issue is that when you invoke getClass() you are calling the method in the super class – static methods are not inherited. In addition, you are basically name-shadowing Object.getClass(), which is confusing. If you need to log the classname within the superclass, use return this.getClass().getName(); This will return “Entity” … Read more

When do you need to explicitly call a superclass constructor?

You never need just super(); That’s what will be there if you don’t specify anything else. You only need to specify the constructor to call if: You want to call a superclass constructor which has parameters You want to chain to another constructor in the same class instead of the superclass constructor You claim that: … Read more

Inheritance and Overriding __init__ in python

The book is a bit dated with respect to subclass-superclass calling. It’s also a little dated with respect to subclassing built-in classes. It looks like this nowadays: class FileInfo(dict): “””store file metadata””” def __init__(self, filename=None): super(FileInfo, self).__init__() self[“name”] = filename Note the following: We can directly subclass built-in classes, like dict, list, tuple, etc. The … Read more

Why aren’t superclass __init__ methods automatically invoked?

The crucial distinction between Python’s __init__ and those other languages constructors is that __init__ is not a constructor: it’s an initializer (the actual constructor (if any, but, see later;-) is __new__ and works completely differently again). While constructing all superclasses (and, no doubt, doing so “before” you continue constructing downwards) is obviously part of saying … Read more

super() raises “TypeError: must be type, not classobj” for new-style class

Alright, it’s the usual “super() cannot be used with an old-style class”. However, the important point is that the correct test for “is this a new-style instance (i.e. object)?” is >>> class OldStyle: pass >>> instance = OldStyle() >>> issubclass(instance.__class__, object) False and not (as in the question): >>> isinstance(instance, object) True For classes, the … Read more