I've noticed that classes that subclass android classes don't require having a constructor and calling the constructor of the superclass. Why is that? I thought all classes except pojo's needed a constructor?
2 Answers
Because you're not overriding the constructor. There's no requirement* saying you need to override the constructor of the superclass.
I thought all classes except pojo's needed a constructor?
They have a constructor. It's inherited from their parent class. You're just not REIMPLEMENTING the constructor.
*As noted by @Christian, you would need to implement a constructor if the parent class's constructor took arguments. In the case of android classes (most), they don't.
5 Comments
Matt Phillips
Ah. I guess that was a fundamental of java that I misunderstood. I thought even if you aren't reimplementing the constructor you still had to call it somehow.
Cristian
There's no requirement saying you need to override the constructor of the superclass. if the super-constructor has parameters, then yes: it's mandatory to override it.
espinchi
Also, you need to call the constructor of your superclass if it sets private final fields.
Stewart Murrie
Just being picky, but you can't actually override the constructor of a superclass :) You can only write a constructor for your child class which calls the constructor of a superclass.
fadden
I'll pile on to the previous nit pick: there is no constructor inheritance. The compiler will politely generate a no-arg constructor that calls the superclass constructor. If you dump the bytecode you can see what it's doing.