0
  @Override
  public boolean add( Object o )
  { 
    return super.add( o );
   // Sorts arraylist
    Collections.sort(this, new Comparator<Object>() {
    // code here
    }
   });
  }
}

As you can see, I am trying to @Override the method add, found in the superclass, and implement Collections.sort() in the subclass. I have added a comparator to help implement this however, it says that the code is unreachable.

Any suggestions would be appreciated.

mexicanChica
  • 69
  • 1
  • 9
  • 1
    The error is because the first line of your `add` method is `return`, so anything beyond that is unreachable. Also, it's unlikely you want to have a class `extend` `ArrayList`. – GriffeyDog Dec 19 '17 at 15:40
  • The method will not run after it returns. Put the code before returns. – clinomaniac Dec 19 '17 at 15:40
  • 1
    `return super.add( pr );` returns so the other lines of code are not reachable. Learning about inheritance is good, but extending ArrayList is not! Composition is likely more appropriate in your example. – Andrew S Dec 19 '17 at 15:43

3 Answers3

3

You have a return statement as the first statement, so anything following it is unreachable code:

public boolean add( Product pr )
{ 
    return super.add(pr);
    Collections.sort(this, new Comparator<Product>() { // unreachable
    @Override
    public int compare(Product p1, Product p2) {
        double f = p1.getPrice();
        double s = p2.getPrice();
        if (f == s) return 0;
        return f<s ? 1 : -1;
    }
   });
}

Since List.add always returns true, you can safely ignore the value returned by super.add(pr) and add a return statement after sorting the List:

public boolean add( Product pr )
{ 
    super.add(pr);
    Collections.sort(this, new Comparator<Product>() {
    @Override
    public int compare(Product p1, Product p2) {
        double f = p1.getPrice();
        double s = p2.getPrice();
        if (f == s) return 0;
        return f<s ? 1 : -1;
    }
   });
   return true;
}
Eran
  • 387,369
  • 54
  • 702
  • 768
2

In this instance you have

return super.add( pr );

before the rest of the code you would like to execute. Return terminates the method imediatally so all the code you have after it will never run which is why you are getting the unreachable error. You can just remove the return from the super.add() to get rid of this error

Tevin
  • 21
  • 1
1

The problem is the return super.add( pr ) you are doing. The return keyword returns the value and ends the execution of the function, making sure your code is never executed

public class BetterBasket extends Basket implements Serializable
{
  private static final long serialVersionUID = 1L;

  @Override
  public boolean add( Product pr )
  { 
    return super.add( pr ); // returns the value returned by super.add( pr ) and ends the function
    Collections.sort(this, new Comparator<Product>() {
    @Override
    public int compare(Product p1, Product p2) {
        double f = p1.getPrice();
        double s = p2.getPrice();
        if (f == s) return 0;
        return f<s ? 1 : -1;
    }
   });
  }
}
Ayush Gupta
  • 8,716
  • 8
  • 59
  • 92