-5
public class Fish 
{
// Any fish below this size must be thrown back into the lake
public static int  THROW_BACK_SIZE = 18; 

protected int  size;
protected float  weight;



public Fish(int aSize, float aWeight) 
{
    size = aSize;
    weight = aWeight;
}



    public boolean canKeep() 
{
  if (size>THROW_BACK_SIZE){
    return true;
  }
  else{

    return false;
}
}
    public boolean isDesirableTo(Fisher f) 
{
      if(size>THROW_BACK_SIZE && Fisher.numFishCaught<Fisher.LIMIT){
        return true;
      }
      else{
    return false;
}
    }


public int getSize() { return size; }
public float getWeight() { return weight; }

public String toString () 
{
    return ("A " + size + "cm " + weight + "kg Fish");
}

}

This is what i currently have under my fish class. I am now suppose to make two subclasses, NonEndangeredFish and EndangeredFish. Under NonEndangeredFish has the subclasses Perch and Bass, while EndangeredFish has AuroraTrout and AtlanticWhiteFish. Note that Fish, NonEndangeredFish and EndangeredFish are all abstract classes, while the other 4 are concrete classes. With the exception of the Fish class, none of the other classes here have any attributes defined within them. This is what I tried to put under my fish class but was unsuccessful.

public abstract class NotEndangeredFish extends Fish{



}

I got this error:

Error: class NotEndangeredFish is public, should be declared in a file name NotEndangeredFish.java

Any help on how to do this properly would be greatly appreciated. Thanks!

3 Answers3

2

Each public class (except inner) must be declared in separate file named according to class.

Leos Literak
  • 8,805
  • 19
  • 81
  • 156
1

The error is pretty clear. class NotEndangeredFish is public, so it should be declared in a different file named NotEndangeredFish.java.

Why is each public class in a separate file?

This restriction implies that there must be at most one such type per compilation unit. This restriction makes it easy for a compiler for the Java programming language or an implementation of the Java virtual machine to find a named class within a package; for example, the source code for a public type wet.sprocket.Toad would be found in a file Toad.java in the directory wet/sprocket, and the corresponding object code would be found in the file Toad.class in the same directory.

See this for further information.

Community
  • 1
  • 1
Christian Tapia
  • 33,620
  • 7
  • 56
  • 73
0

Just like the error says, you must make the name of the NotEndangeredFish file to be NotEndangeredFish.java.

BitNinja
  • 1,477
  • 1
  • 19
  • 25