Java Inheritance – calling superclass method
You can do: super.alphaMethod1(); Note, that super is a reference to the parent class, but super() is its constructor.
You can do: super.alphaMethod1(); Note, that super is a reference to the parent class, but super() is its 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
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
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
Yes, it is, the syntax is close to C#, here is an example with both default constructor and named constructor: class Foo { Foo(int a, int b) { //Code of constructor } Foo.named(int c, int d) { //Code of named constructor } } class Bar extends Foo { Bar(int a, int b) : super(a, b); … Read more
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
If you need something from super’s __init__ to be done in addition to what is being done in the current class’s __init__, you must call it yourself, since that will not happen automatically. But if you don’t need anything from super’s __init__, no need to call it. Example: >>> class C(object): def __init__(self): self.b = … Read more
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
Just use the < operator B < A # => true A < A # => false or use the <= operator B <= A # => true A <= A # => true
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