0

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 2

3

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.

Sign up to request clarification or add additional context in comments.

5 Comments

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.
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.
Also, you need to call the constructor of your superclass if it sets private final fields.
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.
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.
1

Well... those classes you are talking about have a default constructor (one with no parameters). If they had parameters (like the View class), then you MUST override at least one constructor.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.