• Post Reply Bookmark Topic Watch Topic
  • New Topic

Duplicate method  RSS feed

 
Niall McArea
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The question asks for me to create an invoice class with instance variables,part,part description price and quantity (String,string double and int) with setters getters and constructors for each variable.However when I go to create a second string constructor eclipse claims I've a duplicate method.What am I doing wrong or how do I get around that?





 
K. Tsang
Bartender
Posts: 3648
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

The error Eclipse is complaining is because the java compiler can't distinguish which constructor to call if you pass in only 1 string (parameter). Is it Invoice(number) or Invoice(description)?

Also constructors are not the same as methods.

Looking at your requirement, I think you misunderstood the phrase
..... constructors for each variable


For the instance variables "price" and "quantity" can be misleading. Price as in for all items bought? Quantity as in total number of items?

You can indeed have multiple constructors but the number of parameters must be different. This is call overloading. So your constructors can look like the following depending on what is provided:


If you don't have the default constructor then your code can not do stuff like:


Hope my explanation helps.
 
Paweł Baczyński
Bartender
Posts: 2077
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch!

Please UseCodeTags (← click this) when posting.
I added them for you this time. Doesn't it look better?

You do not have duplicate methods.
You do have duplicate constructors
public Invoice (String number)
and
public Invoice (String description)

The name of the parameter does not matter to the Java compiler so they have exactly the same signature.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!