C. Anderson C. Anderson - 1 year ago 64
Java Question

Implementing abstract methods in a concrete class

I have coursework for my University project and I am struggling with the concept of abstract classes. I realise the reason for having them, to create sub-classes and I understand simple abstract class examples. The question is asking to implement a concrete class, Selector, which implements the abstract methods.

Create an abstract base class, ASelector, with two abstract methods called selectorRange and selectorValue as follows:

  1. the selectorRange method requires two parameters that define the integer range of possible values from min to max (inclusive) with the following signature
    public abstract void selectorRange( int min, int max );

  2. the selectorValue method returns a randomly generated value in the range from min to max (inclusive) and has the following signature
    public abstract int selectorValue( );

Create a concrete class, Selector, that implements ASelector with appropriate code to implement both methods.

So far I have:

public class Selector extends ASelector {

public void selectorRange(int min, int max) {
min = 1;
max = 10;

public int selectorValue(int min, int max) {

Random r = new Random();
int randomNumber = r.nextInt(max - min) + min;

return randomNumber;


But the error I am getting is with Selector in the method header. It says I have not implemented the abstract methods, yet I have? Or I think I have. Any help would be appreciated.

My ASelector class:

public abstract class ASelector {

public abstract void selectorRange(int min, int max);
public abstract int selectorValue();

Answer Source

In your class selector you should store the max and min number

     int max;
     int min;

Then your selectorValue code should look like this:

      public int selectorValue(){
          Random r = new Random();
          int randomNumber = r.nextInt(this.max - this.min) + min;
          return randomNumber;

What happens is that java allows overload of methods, this means, you can create methods with the same name and different input atrributes and they will be different. This means that public int selectorValue() is different formpublic int selectorValue(int min, int max), so the method is not implemented.

As a consequence of storing max and min in the Selector your selectorRange should be like this:

    public void selectorRange(int min, int max) {
        this.min = min;
        this.max = max;  

This will set your internal min and max variables to the value of the parameters min and max