Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Abstract class with default value

I am trying to define a abstract Range class which will serve as the base implementation of a number of range classes. The intended use is irrelevant for this question, but so far I have:

/**
 * Abstract generic utility class for handling ranges
 */
public abstract class Range<T extends Number> {

  // Variables to hold the range configuration
  private T start;
  private T stop;
  private T step;

  /**
   * Constructs a range by defining it's limits and step size.
   *
   * @param start The beginning of the range.
   * @param stop The end of the range.
   * @param step The stepping
   */
  public Range(T start, T stop, T step) {
    this.start = start;
    this.stop = stop;
    this.step = step;
  }
}

Now I want to add a constructor with that only takes start and stop, and sets a default step value of 1, no matter what implementation of Number T is (e.g. if T is Integer [one] would have the value 1, and if T is Long [one] would have the value 1L, and so on). I would like something like:

protected Range(T start, T stop) {
  this(start, stop, [one]);
}

but I can't figure out how so set the value of [one]. Since Java is still fairly new to me, I have tried with:

private static final T one = 1;

which doesn't work because T is obviously defined in the instantiation of Range.this. Also I have tried:

protected static abstract T getOne();

which also doesn't work because T is defined in the instantiation of Range.this plus static and abstract don't work together.

I need some way for extending classes to be forced to define the value of [one] no matter what implementation of Number Range is implemented for.

Ultimately I would also like to set a zero value as default start, such that I get a constructor that looks like this:

protected Range(T stop) {
  this([zero], stop, [one]);
}
like image 847
beruic Avatar asked May 23 '14 09:05

beruic


People also ask

Can abstract class have default method?

An abstract class can have abstract and non-abstract methods. From Java 8, it can have default and static methods also.

Is abstract class public by default?

Abstract Classes Compared to Interfaces With interfaces, all fields are automatically public, static, and final, and all methods that you declare or define (as default methods) are public.

Why default methods are not allowed in abstract class?

As we know, an interface can't have a state, and therefore, the default method can't access the state.

Can abstract class have default and static methods?

1) Abstract class can have abstract and non-abstract methods. Interface can have only abstract methods. Since Java 8, it can have default and static methods also.


2 Answers

One solution would be to ask the subclasses for the default step to use:

public abstract class Range<T extends Number> {

    private T start;
    private T stop;
    private T step;

    public Range(T start, T stop, T step) {
        this.start = start;
        this.stop = stop;
        this.step = step;
    }

    protected Range(T start, T stop) {
        this.start = start;
        this.stop = stop;
        this.step = getDefaultStep();
    }

    protected abstract T getDefaultStep();

}

public class IntegerRange extends Range<Integer> {

    public IntegerRange(Integer start, Integer stop, Integer step) {
        super(start, stop, step);
    }

    public IntegerRange(Integer start, Integer stop) {
        super(start, stop);
    }

    @Override
    protected Integer getDefaultStep() {
        return 1;
    }

}

public class DoubleRange extends Range<Double> {

    public DoubleRange(Double start, Double stop, Double step) {
        super(start, stop, step);
    }

    public DoubleRange(Double start, Double stop) {
        super(start, stop);
    }

    @Override
    protected Double getDefaultStep() {
        return 1d;
    }

}
like image 60
sp00m Avatar answered Oct 06 '22 01:10

sp00m


You can use this implementation:

protected Range(T start, T stop) {
  this(start, stop, (T) (new Integer(1)));
}
like image 43
Dmitry Bychenko Avatar answered Oct 06 '22 01:10

Dmitry Bychenko