+ 2

Implementing sub-classes & their constructors

So, in my course in uni we're moving through the implementation and use of sub-classes, but I've hit a bit of a bump on something. So my issue is trying to understand inheritance and constructor behavior between the Parent and its sub-classes in java. I'm familiar with the concept of objects and sub-classes, and how it's suppose to work (as I use them regularly in other languages). That being said, I'm trying to understand the proper syntax for java. There is really only two big questions, maybe some explanation of the behavior is welcome too: 1) When creating a Parent class, can (should) it contain its own constructor? What are the limitations? 2) Do the sub-classes inherit the attributes declared in the parent class?

22nd Oct 2017, 9:01 AM
Sapphire
3 Answers
+ 2
1) You can choose to either have a constructor or not, what do you mean by limitations? 2) Sub-classes inhert all the attributes and methods besides the private ones.
22nd Oct 2017, 1:07 PM
Chriptus13
Chriptus13 - avatar
+ 1
1) the issue I'm having when playing around with it is when I have a parent class with its own constructor with parameters set, and I have a sub-class with its own constructor as well - it gives an error "no default constructor for "parentname"". So I'm trying to figure out what is the correct way of doing it.
22nd Oct 2017, 1:49 PM
Sapphire
+ 1
Oh you have to call the constructor of the parent class Aka super()
31st Oct 2017, 7:16 PM
Chriptus13
Chriptus13 - avatar