fork download
  1. import java.util.List;
  2. import java.util.LinkedList;
  3.  
  4. abstract class Base {
  5. private List<String> strings;
  6.  
  7. public Base() {
  8. this.strings = new LinkedList<String>();
  9. }
  10.  
  11. protected void remember(String s) {
  12. this.strings.add(s);
  13. }
  14.  
  15. public int rememberedCount() {
  16. return this.strings.size();
  17. }
  18. }
  19.  
  20. class Derived extends Base {
  21. public Derived() {
  22. super();
  23. this.remember("one");
  24. this.remember("two");
  25. // Would fail with error:
  26. // this.strings.add("three");
  27. }
  28. }
  29.  
  30. class Test {
  31. public static void main(String[] args) {
  32. Derived d = new Derived();
  33. System.out.println(d.rememberedCount()); // 3
  34. }
  35. }
Success #stdin #stdout 0.1s 320512KB
stdin
Standard input is empty
stdout
2