Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: overloading an equals method

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default overloading an equals method

    My assignment for class is to create a base class and a subclass, and each one has to have an equals method. Both equals methods are meant to iterate through the indices of an array, but the base class method is for integers and the derived class method is for strings. The equals method in the derived class is supposed to overload the equals method in the base class, but not override it. Right now, the equals method I have in my base class is:

    public void equals(int[] equalsList) {
    boolean found = false;
    for (int i = 0; i < numbers.length; i++) {
    equalsList[i] = numbers[i];
    }
    for (int i = 0; i < equalsList.length; i++) {
    for (int j = 1; j > i && j < equalsList.length; j++) {
    if(Integer.toString(equalsList[i]).equalsIgnoreCase(Integer.toString(equalsList[j]))) {
    found = true;
    break;
    }
    }
    if (found) {
    System.out.println("You entered a duplicate number. Please restart and only enter each number once.");
    System.exit(0);
    }
    }
    }

    When I try to pass an array as a parameter in the derived class (ex. super.equals(priorities);, where priorities is an array containing a list of integers), it doesn't work and I still get duplicate numbers. I was able to make it work when I had no parameters at all for the base class equals method, but I need it to have a parameter so it can be overloaded by the equals method of the derived class. Does anyone have any suggestions?

    Thanks!!


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: overloading an equals method

    it doesn't work
    Please explain.
    Can you make a small program that compiles, executes and shows the problem? Include a main() with testing data.

    Please edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. create an equals() method that overrides the object equals() method
    By slinky29 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 3rd, 2013, 11:11 PM
  2. Abstract method vs overloading
    By tcstcs in forum Java Theory & Questions
    Replies: 4
    Last Post: January 10th, 2013, 02:58 AM
  3. Overloading Method
    By Tohtekcop in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 16th, 2012, 02:28 PM
  4. Advantages of method Overloading and Overriding
    By tcstcs in forum Java Theory & Questions
    Replies: 2
    Last Post: January 19th, 2012, 04:55 AM
  5. Method Overloading - Doubt
    By vidya lakshman in forum Java Theory & Questions
    Replies: 2
    Last Post: January 31st, 2011, 09:32 AM