I have an interface class and in this class I need to create an abstract method that generates a random int. However, when I try to compile I get an error because abstract classes cannot have bodies. How can I create an abstract method that generates a random int? I also need to specify an upper limit (I said 40).
{
/**
* This method generates a random number.
*
* @param y a sample parameter for a method
* @return the result produced by sampleMethod
*/
Random rnd = new Random();
System.out.println(rnd.nextInt(40));
}
You said it yourself, abstract methods can't have bodies.
I suggest you write an absract class that has a concrete method that produces your random
int
and then have classes that extend from your abstract class.Remember that an interface can only have abstract methods and static final variables.