Given this class definition:
abstract class Base { public abstract Number getValue(); }
Which of the following two options are correct concrete classes extending Base class?
a)class Deri extends Base { protected Number getValue() { return new Integer(10); }// w ww . ja v a 2s. com } b)class Deri extends Base { public Integer getValue() { return new Integer(10); } } c)class Deri extends Base { public Float getValue(float flt) { return new Float(flt); } } d)class Deri extends Base { public java.util.concurrent.atomic.AtomicInteger getValue() { return new java.util.concurrent.atomic.AtomicInteger(10); } }
b) d)
option b) makes use of a co-variant return type (note that Integer extends Number), and defines the overriding method correctly.
option d) makes use of co-variant return type (note that AtomicInteger extends Number), and defines the overriding method correctly.
option a) attempts to assign a weaker access privilege by declaring the method protected when the base method is public, and thus is incorrect (results in a compiler error).
In option c) the method Float getValue
(float flt) does not override the getValue()
method in Base since the signature does not match, so it is incorrect (results in a compiler error).