Meaningless Drivel is fun!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes constructor Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "constructor" Watch "constructor" New topic
Author

constructor

binay shah
Greenhorn

Joined: Jan 19, 2013
Posts: 14
can any one tell why the code below does not compile?

The solution to this problem says "When extending a class always ensure that it has a public constructor, since the compiler will try to call it by default ".
when i put the default constructor in class Marmaduke it compiles.
but why does it need a default constructor when there isn't a subclass object instantiation with no arguments
thanks in advance.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

binay shah wrote:



subclass Cert has a default constructor, since you dont construct constructor explicitly . and the default constructor calls no argument super class's constructor as a first statement
as in below
binay shah
Greenhorn

Joined: Jan 19, 2013
Posts: 14
thanks for your response.
i understand that there is a implied super() call in the Cert class but line 8 is calling the super class constructor directly. My understanding of the above code is that - "if line 8 was replaced by 'new Cert()', then there is a constructor chaining.
The problem i'm not understanding is that, where is the call to subclass constructor?
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3432
    
  12
binay shah wrote:The problem i'm not understanding is that, where is the call to subclass constructor?

There is no call to the subclass constructor, but you have not included a constructor in the subclass and so the compiler automatically includes a no-arg constructor. The only thing this constructor does is call the no-arg constructor of its parent class. But the parent class does not have a no arg constructor, because you have included a 1 arg constructor in the parent class, so the compiler does not add a no arg constructor. It's the lack of a no arg constructor in the parent class that is called from the no arg constructor of the sub class that the compiler is complaining about.


Joanne
binay shah
Greenhorn

Joined: Jan 19, 2013
Posts: 14
Thank you very much. I got it!
 
wood burning stoves
 
subject: constructor
 
Similar Threads
constructor up and down hierarchy
Co variant returns
problem in certpal mock ?
Why Is a No-Arg Constructor Required Here?
null a valid subtype of object ?