• 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
  • Devaka Cooray
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Jeanne Boyarsky
  • Tim Cooke
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
Bartenders:

Constructor confusion

 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does every class have to have a default constructor if you specify overloaded constructors?

I created a case when my main programs creates an object of class A. Class A has ONLY 1 overloaded constructor taking 1 argument. As long as I make sure my main program creates the object and passes the argument. It will be okay. But obviously if I create the object without any parameters it will not compile.

Thanks
 
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It doesnt have to have default constructor, but if you do not define a constructor compiler will provide you with a default constructor, which is no-arg constructor. So in nutshell I guess, you can supply your own constructor but if you dont supply compiler will do it for you.
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Whenever a constructor is overloaded compiler does not provide default no argument constructor and hence make sure that we create an object using the constructors that are provided.

Cheers,
Murali
 
Fran Kindred
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry forgot to mention that I mean no arg constructor. Does every class have to have a no arg constructor.

I asked this question because I found a case where I dont need a no arg constructor but I do have a overloaded constructor.

Also EVERY constructor has to have a this() or a super() correct?
 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry forgot to mention that I mean no arg constructor. Does every class have to have a no arg constructor.

if you write even one constructor for the class than there will no arg constructor provided by the compiler. In that case you need to write the no arg constructor if you need it.


Also EVERY constructor has to have a this() or a super() correct? [/LIST]
for super(), even if you dont specify it, the compiler will keep it for you on the first line just after your constructor definition. This doesnt apply to this keyword.
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Every constructor needs to have a call to super(), but if you forget it the compiler will put the call there for you. Remember however that if you do put it in your constructor, it must be it's first statement. The call to this() is not necessary, only if you want to call a overloaded contructor.
 
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Another detail that may make it onto the exam:

If you code at least one constructor in your class and you do not provide a no-arg constructor, than your class will have no no-arg constructor.

This means that "MyClass myObject = new MyClass();" will fail to compile.

It also means that if MyClass has any subclasses, every constructor in the subclass must start with
"this( ... );" or with "super( parameters to match a MyClass constructor );"

If you let the compiler insert the first line by default, it will insert
"super()". Since there is no matching "MyClass()" constructor, compilation will fail.
[ November 19, 2004: Message edited by: Mike Gershman ]
 
Fran Kindred
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So in summary:

Every constructor must have a this() or super().
super() will be inserted for you if you dont provide but will fail to compile if you include it but its not in the beginning.
If you dont supply any constructors a default no arg will be provided
If you supply only other overloaded constructors make sure that there is no
creation of the object without parameters or else it will fail.

Anything else?

thanks to all that replied
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

super() will be inserted for you if you dont provide but will fail to compile if you include it but its not in the beginning.


A constructor with super() will fail to compile if there is no no-arg constructor in the superclass.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think this question has been answered above, but perhaps I can summarize with some further clarification...

A no-args constructor is not required.

The compiler will provide a default no-args constructor only if no other constructor is defined. (Exam tip: If the class is public, then the compiler-provided constructor is also public. Otherwise, the compiler-provided constructor will have default access.)

If an overridden version of super() is not called explicitly as the first line of a constructor, then an implicit call to super() is made with no arguments. If super() is called with no arguments, then the parent class must have a no-args constructor (either explicitly defined or automatically provided).

A no-args constructor does nothing other than call super(). (EDIT: Whoops... See Mike Gershman's clarification on this below.)

If this(args) is used as the first line of a constructor, then the overloaded version of that constructor is called. (Note that "this" is used because calling another constructor explicitly would result in another object.) A compile-time error will result if a constructor attempts to call itself, either directly or indirectly.

Since either must appear as the first line, super(args) and this(args) cannot both appear in a constructor.
[ November 19, 2004: Message edited by: marc weber ]
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
read this Carefully, it "may" help you out...!!
http://www.geocities.com/sunjava_scjp/constructor-tips.html
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Marc Weber:

A no-args constructor does nothing other than call super().


The default constructor, which is a no-arg constructor, does nothing but call super(). You can code a no-arg constructor yourself that does a great deal of work or does nothing. If you code any constructor for a class, there will be no default constructor but you may supply a no-arg constructor if you wish.
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Mike Gershman:
The default constructor, which is a no-arg constructor, does nothing but call super(). You can code a no-arg constructor yourself that does a great deal of work or does nothing.


Yes, of course you are correct. I neglected to specify "default."
 
It's never done THAT before. Explain it to me tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic