fork(6) download
  1. interface Collection<T> {
  2. void add(T t);
  3. void addRange(T[] ts);
  4. }
  5.  
  6. abstract class AbstractCollection<T> implements Collection<T> {
  7. @Override
  8. public void addRange(T[] ts) {
  9. for (T t : ts) {
  10. add(t);
  11. }
  12. }
  13. }
  14.  
  15. class CollectionWithCounter<T> extends AbstractCollection<T> {
  16. int elementsCount = 0;
  17.  
  18. @Override
  19. public void add(T t) {
  20. elementsCount++;
  21. }
  22.  
  23. @Override
  24. public void addRange(T[] ts) {
  25. elementsCount += ts.length;
  26. super.addRange(ts);
  27. }
  28. }
  29.  
  30. class Main {
  31. public static void main(String[] args) {
  32. CollectionWithCounter<Object> cc;
  33.  
  34. cc = new CollectionWithCounter<>();
  35. cc.add(null);
  36. cc.add(null);
  37. cc.add(null);
  38. System.out.println("elementsCount = " + cc.elementsCount);
  39.  
  40. cc = new CollectionWithCounter<>();
  41. cc.addRange(new Object[3]);
  42. System.out.println("elementsCount = " + cc.elementsCount);
  43. }
  44. }
Success #stdin #stdout 0.04s 711168KB
stdin
Standard input is empty
stdout
elementsCount = 3
elementsCount = 6