fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.io.*;
  4. import java.lang.*;
  5. import java.util.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone {
  9. interface Tapelibrary<T extends TapeDrive> {
  10. List<T> getListofDrives();
  11.  
  12. void doSomethingWithDrive(T d);
  13. }
  14.  
  15. static class SpecificTapeLibrary implements Tapelibrary<HPDrive> {
  16. private List<HPDrive> driveList;
  17.  
  18. SpecificTapeLibrary() {
  19. driveList.add(new HPDrive());
  20. driveList.add(new HPDrive());
  21. driveList.add(new HPDrive());
  22. }
  23.  
  24. @Override
  25. public List<HPDrive> getListofDrives() {
  26. return driveList;
  27. }
  28.  
  29. @Override
  30. public void doSomethingWithDrive(HPDrive d) {
  31. d.doSomethingHPspecific();
  32. }
  33. }
  34.  
  35. abstract static class TapeDrive {
  36. void doSomething() {}
  37. }
  38.  
  39. static class HPDrive extends TapeDrive {
  40. void doSomethingHPspecific() {}
  41. }
  42.  
  43. static <T extends TapeDrive> void doStuff(Tapelibrary<T> t) {
  44. if (t == null) return;
  45.  
  46. List<T> listOfDrives = t.getListofDrives();
  47.  
  48. // the user selects a drive by using a small UI or something
  49. T selectedDrive = listOfDrives.get(0);
  50.  
  51. t.doSomethingWithDrive(selectedDrive);
  52. }
  53.  
  54. public static void main(String[] args) throws java.lang.Exception {
  55. Tapelibrary<? extends TapeDrive> t = null;
  56. doStuff(t);
  57. }
  58. }
Success #stdin #stdout 0.03s 711168KB
stdin
Standard input is empty
stdout
Standard output is empty