• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

no-arg default constructor ?

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi I am preparing for SCJP 6
I came across this question(SCREENSHOT ATTACHED) on the JAVA TM 6 Study Guide (accompanied with SCJP 6 book by Kathy Sierra and Bert Bates) while trying a mock exam.
As per my knowledge, if a constructor with arguments is defined (as highlighted), then JVM wont add the default no-arg constructor . So the call to no-arg constructor should give a compile time error, But according to the answers given, it runs fine . And gives output ( -3,-1 and 5) .

Thanks in Advance
Arshpreet
SCJPQ55.PNG
[Thumbnail for SCJPQ55.PNG]
A screenshot for Q55
 
Ranch Hand
Posts: 208
9
Eclipse IDE Firefox Browser Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are correct, the code should give a compile time error.
If there is a constructor defined, the default constructor is not created unless you explicitly define it.

Welcome to the Ranch!
 
Arshpreet Lamba
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Tina
 
Arshpreet Lamba
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have another query form the same quiz I asked last time , SCJP TM 6 Study Guide by Bert Bates and Kathy Seirra .In this question , a Comparator is initialized like this : new Comparator<String>. (highlighted in the screenshot) . Is it possible to instantiate an interface like that ? Does the interface Comparator has a no-arg constructor???

Thanks in advance
Q54.PNG
[Thumbnail for Q54.PNG]
 
Tina Smith
Ranch Hand
Posts: 208
9
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes. What you are doing is creating an anonymous inner class which implements the interface. And that anonymous class, because there is no constructor defined, will have the no-arg constructor.

Interfaces have no control over the constructor for the implementing class. Probably because a constructor relates to how a class is implemented, while interface just defines what those who implement it will do (not how it will do it).
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Arshpreet Lamba wrote:
As per my knowledge, if a constructor with arguments is defined (as highlighted), then JVM wont add the default no-arg constructor . So the call to no-arg constructor should give a compile time error, But according to the answers given, it runs fine . And gives output ( -3,-1 and 5) .



Unfortunately, while your argument is correct, your conclusion is wrong. Yes, the class will not have a default constructor. However, the call to the no-arg constructor will call the constructor defined -- meaning that it should compile. Remember that with var-args, you can have no arguments too.

Henry
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Arshpreet Lamba wrote:I have another query form the same quiz I asked last time , SCJP TM 6 Study Guide by Bert Bates and Kathy Seirra.



Please start a new topic for different questions. It makes it harder to read when you use a single topic for different discussions.

Henry
 
Tina Smith
Ranch Hand
Posts: 208
9
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Whoops, Henry is right. I missed that it was a varargs constructor...
 
Arshpreet Lamba
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh I am new to the site . Didn't realise that . Sorry .

And thanks Tina and Henry for you time .I appreciate that
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic