1

Hello I am building an book store and I receive this error message

Duplicate method getApparel(String) in type Bookstore

Apparel is a class in these methods. How I fix this. Eclipse suggest to rename these.

public Apparel getApparel(String id)
{
    for(int i=0; i < apparel.size(); i++)
    {
        if(apparel.get(i).getId().equals(id))
        {
            return apparel.get(i);
        }
    }

    return null;
}

public ArrayList<Apparel> getApparel(String name)
{
    ArrayList<Apparel> apparel = new ArrayList<Apparel>();
    for(int i=0; i < this.apparel.size(); i++)
    {
        if(this.apparel.get(i).getName().equals(name))
        {
            apparel.add(this.apparel.get(i));
        }
    }

    return apparel;
}
1
  • You can probably simplify your for loops with Linq. Commented Apr 23, 2013 at 2:00

4 Answers 4

3

In Java, you cannot have multiple methods in the same class with the same method name and same argument types and count - the different return values are not considered in this context.

To resolve the issue, you must either rename one of the methods, or add or change the arguments so they're unique.

Sign up to request clarification or add additional context in comments.

Comments

0

Something like this:

public Apparel getApparelById(String id)
{
    for(int i=0; i < apparel.size(); i++)
    {
        if(apparel.get(i).getId().equals(id))
        {
            return apparel.get(i);
        }
    }

    return null;
}

public ArrayList<Apparel> getApparelByName(String name)
{
    ArrayList<Apparel> apparel = new ArrayList<Apparel>();
    for(int i=0; i < this.apparel.size(); i++)
    {
        if(this.apparel.get(i).getName().equals(name))
        {
            apparel.add(this.apparel.get(i));
        }
    }

    return apparel;
}

Comments

0

Both methods have the same parameter types, but different return type and this is not possible. From Java Language Specification:

Two methods have the same signature if they have the same name and argument types.

If both methods has different parameter types, then it is possible.

Comments

0

Both of the methods getApparel(String) has the SAME METHOD SIGNATURE! JAVA and C++ DISALLOW multiple methods with the same method signature. Otherwise, the compiler wound't recognize which method is to invoke.

Method signature is identified by: method name + method parameter list(type list). Compiler doesn't differentiate methods by return type.

For detailed information you can refer to: Defining Method

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.