aspose file tools*
The moose likes Beginning Java and the fly likes Overloaded constructors gives Duplicate method in type error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Overloaded constructors gives Duplicate method in type error" Watch "Overloaded constructors gives Duplicate method in type error" New topic
Author

Overloaded constructors gives Duplicate method in type error

Patrick de Kruijf
Ranch Hand

Joined: Mar 02, 2010
Posts: 63
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){}'.



------------------8<------------------
please cut here
Raymond Tong
Ranch Hand

Joined: Aug 15, 2010
Posts: 230
    
    2

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

Vinoth Kumar Kannan
Ranch Hand

Joined: Aug 19, 2009
Posts: 276

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!


OCPJP 6
Patrick de Kruijf
Ranch Hand

Joined: Mar 02, 2010
Posts: 63
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

Joined: Aug 19, 2009
Posts: 276

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

Joined: Mar 02, 2010
Posts: 63
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

Joined: Aug 19, 2009
Posts: 276

You're welcome
Java always is fun! All the bestttt!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Overloaded constructors gives Duplicate method in type error