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

2 Construtors for 1 class?

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Forgive my question, I am new to Object Oriented programming and Java, but not to programming. I am having a little trouble with the constructor concept. A constructor is a method with the same name as the class, correct? It is what really creates the class, correct?
I was looking at an example where a class had 2 different constructors, each was different in the parameter list by having different types and numbers of parameters. How can this be, I don't understand this can someone explain why people do this, what is it used for? Thanks
 
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Thomas,
i'd rely on somebody else for detailed explanation and may be a better one but i've a simple practical example...
imagine that ur mom told u to get something from a grocery store. now, there r two possibilities,
scenario-1: she doesn't tell which grocery store to go
scenario-2: she tell us to go to 'x' grocery store
now the 1st scenario makes u use ur common sense. u will go to the store which is,
1. near by,
2. u already know.
(both these cases assumes that u 'll only think about stores that can have the grocery item being asked. obvious enough).
in 2nd scenario implies that u 'll first try the store u r told (assuming ur in good state of mind to follow mom's advice.. )
(We will ignore the case that will make u go another store if this first store person says-we don't have it)
as u observe- in both the scenario u r ONLY ONE. u follow things based upon the input given to u. in 1st scenario u assumed some "default" values so to say ...(that is store to go)
and in 2nd scenario u got input about the store to go from the external source (ur mom in this case)...
in any scenario u accomplish the task of getting the specified thing from the grocery store, right???
same with OBjects in java (or any other OO). u can have two constructors for the same object as sometimes u want to use "default" values to instantiate the object and sometimes u want to be able to specify "ur own values" to the object initialization...
u can have any number of constructors u want to have..
i hope i am helpful in little way...
regards
maulin....
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
technically speaking,
its better to have multiple constructors sometimes to avoid "code duplication" which is essentially the concept of "code maintenance".
e.g. u had a main constructor (that u wrote first) that had initialization for parameters to its default values. say u have 5 parameters.
now, in ur application u want to allow other objects to interface with this object and let other objects specify certain input at the object creation time. u create another constructor having 2 input parameters as u wanted to allow other objects to specify two inputs out of 5.
still u have to make other 3 parameters initialized to default value as u do in first constructor , right?
so, there r two options now,
1. u duplicate the initiatilization of the other three parameter to their default value in the 2nd constructor
2. u call this() instead in the second constructor.
which is better???
of course 2nd. because if in future u want to change default values for parameters to something else then u have to just change the 1st constructor but if u did follow the 1st option then u'd endup changing both of the constructors, right??? which is liable to errors and bugs when the software grows in size...
i'm sorry that i'm wordy. instead a simple example would be better but i'm poor at that..
regards
maulin.
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A trivial example:

This class has two constructors, the second one is suplied more as a convenience so you do not have to do new Complex( x, 0 ); when you have only the real part. new Complex( x ); does the trick.
Note that constructors are not methods. The creation of such constructors is an example of Constructor Overloading
[ March 28, 2003: Message edited by: Barry Gaunt ]
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, I would argue that constructors ARE methods, just specialized ones that are called in a special way (i.e. with the new keyword). Of course, I think this is just splitting hairs anyway.
However, constructor overloading is very similar to method overloading, so you may want to look into this broader topic to help you understand the more specific example of constructor overloading.
HTH
Layne
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi all,
i would not like to divert the discussion but i agree 100% that constructors are methods with special convention/restrictions (if u will)...

regards
maulin.
 
Ranch Hand
Posts: 2937
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


I was looking at an example where a class had 2 different constructors, each was different in the parameter list by having different types and numbers of parameters. How can this be, I don't understand this can someone explain why people do this, what is it used for?


Constructor creates and initializes an object. If you want to provide different means of object initialization (as a convinience to the user of the class), overload the constructor. Simple as that.
Eugene.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Maulin Vasavada:
i agree 100% that constructors are methods with special convention/restrictions (if u will)...


I believe that it is because of all the "special convention/restrictions" one can say that constructors are not methods. The JLS also distinguishes between methods and constructors in Section 8, and uses the terms "methods or constructors" or "methods and constructors" throughout. I also found that keeping in mind the idea that "constructors are not methods" got me more than a few points when taking the SCJP recently.
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Barry Gaunt:

I believe that it is because of all the "special convention/restrictions" one can say that constructors are not methods. The JLS also distinguishes between methods and constructors in Section 8, and uses the terms "methods or constructors" or "methods and constructors" throughout. I also found that keeping in mind the idea that "constructors are not methods" got me more than a few points when taking the SCJP recently.


All righty then! I guess I should change my way of thinking. Up until now, I have considered constructors as methods, except... (insert any appropriate special conditions here). I can see how these exceptions can lead to the conclusion that constructors aren't methods. Thank you for pointing this out.
 
Thomas Knight
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wow thanks for all the replys guys. It will take me sometime to read through all of them and digest them
 
I wasn't selected to go to mars. This tiny ad got in ahead of me:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic