• Post Reply Bookmark Topic Watch Topic
  • New Topic

Default constructor issue.  RSS feed

 
WeiJie Lim
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The LocalStudent class inherits the Student class. The IDE states an error of "no default constructor in Student class". Why is this so ?

I understand that if a LocalStudent object is created, the default constructor of its superclass (aka Student class) will be called implicitly.

But in this case, there is no LocalStudent object being created, so why is the default constructor of Student being called ?

The default constructor of LocalStudent is also overloaded by the created no-arg constructor containining subjects = null; . So there is no call to the superclass default constructor from the default constructor of LocalStudent.




 
Matthew Brown
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All subclass constructors call a superclass constructor eventually. There are three possible situations:

1. The first line of the constructor has an explicit call to a superclass constructor (e.g. super(...))
2. The first line of the constructor has an explicit call to another constructor in the same class (e.g. this(...)). But that will either call the superclass constructor or have another this(...) call. Eventually a superclass constructor will be called.
3. There is no explicit call. In that case, the compiler inserts a call of super().

So that's what's going on in your LocalStudent constructors. You don't call another constructor, so the no-arg constructor of the parent class is called. The problem is that the parent class Student doesn't have a no-arg constructor, because you've defined one that takes arguments.

The thing is, the compiler will statically check all of this. It doesn't wait for you to try and create a LocalStudent object before throwing an error. It knows that you've defined a class in an invalid way, and so counts that as an error.
 
K. Tsang
Bartender
Posts: 3648
16
Firefox Browser Java Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
WeiJie Lim wrote:
But in this case, there is no LocalStudent object being created, so why is the default constructor of Student being called ?

The default constructor of LocalStudent is also overloaded by the created no-arg constructor containining subjects = null; . So there is no call to the superclass default constructor from the default constructor of LocalStudent.


When you have a default constructor for LocalStudent, it implicitly calls the superclass default constructor. So in code:



Also since you have a LocalStudent(String name, char yrStudy, String question) constructor, you may consider using the Student(String name, char year) as its base, something like:



Why is yrStudy a char? A int is better.
 
WeiJie Lim
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks @Matthew Brown and @K. Tsang for your clear replies.

Am I right to say that there is always a 'hidden' super(); in each subclass constructor ?
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
WeiJie Lim wrote:Am I right to say that there is always a 'hidden' super(); in each subclass constructor ?


Not if you call super(...) or this(...) explicitly yourself. But otherwise, yes.
 
WeiJie Lim
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew Brown wrote:

Not if you call super(...) or this(...) explicitly yourself. But otherwise, yes.


Oh yeah, thanks for the clarification
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!