• 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

Overloaded constructors gives Duplicate method in type error

 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello, how are you doing? I hope you can help me with my problem.

For my school I need to write a goose class with overloaded constructors. The assignment says I need to make constructors with parameters (int weight), (int age) and (int age, int weight).

When I add the constructors to my code, Eclipse sends back an error on line 8 and 12 saying: "Hey, Patrick, you fool! Listen up: Duplicate method Goose(int) in type Goose ". I played around with the code and when I remove the constructor 'public Goose(int weight){}' the error is gone. But my book says I need to add that constructor. What am I doing wrong?

The code below gives error 'Duplicate method Goose(int) in type Goose' on line 8 and 12.



The code below doesn't give the error after removing constructor 'public Goose(int weight){}'.

 
Ranch Hand
Posts: 258
2
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Constructors or methods can't have same signature.
Signature means same number of parameters with same type in same order.
Parameter name is neglected.

Same signature as

 
Ranch Hand
Posts: 276
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In your first code, when you say new Goose(5) which constructor do you think will be invoked?
Can you see the duplication now? You are having 2 constructors with the same signature - public Goose(int).
Overloading must be done with different parameter types/different no. of parameters/different parameter & different return type.
May be you can make your weight as a double.

Raymond...you beat me by 7 seconds!
 
Patrick de Kruijf
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks everybody, I understand the problem, but I don't understand why it's not possible.

Why can't I say I want to make an object of goose from wich I only know the weight and make another object of that same Goose from which I only know the age? What am I missing here?
 
Vinoth Kumar Kannan
Ranch Hand
Posts: 276
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Patrick de Kruijf wrote:Why can't I say I want to make a object of goose from wich I know the weight and make another object of that same Goose from which I know the age? What am I missing here?


As I already explained, if you say new Goose(5), the compiler knows it must call the constructor which takes a single int parameter, but the problem here is that, the compiler will get confused on which of the 2 constructors that exactly take a single int as a parameter to call. This duplication will cause ambiguity and we programmers must avoid it.
 
Patrick de Kruijf
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your right (duh!), I understand now, the compiler doesn't know what constructor to choose if I say Goose goose = new Goose(5);.

I'm really starting to like learning Java, and I'm sure it's because of this forum. Thanks again!
 
Vinoth Kumar Kannan
Ranch Hand
Posts: 276
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome
Java always is fun! All the bestttt!!
 
Eat that pie! EAT IT! Now read this tiny ad. READ IT!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic