Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2017
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.79 KB | None | 0 0
  1. class SuperClass
  2. {
  3. public static void runStatic(String code)
  4. {
  5. System.out.println(code + ": I am the runStatic method in SuperClass.");
  6. }
  7.  
  8. public static void runStatic2(String code)
  9. {
  10. System.out.println(code + ": I am the runStatic2 method in SuperClass.");
  11. }
  12.  
  13. public void run(String code)
  14. {
  15. System.out.println(code + ": I am the run method in SuperClass.");
  16. }
  17. }
  18.  
  19. class SubClass extends SuperClass
  20. {
  21. public static void runStatic(String code)
  22. {
  23. System.out.println(code + ": I am the runStatic method in SubClass.");
  24. }
  25.  
  26. @Override
  27. public void run(String code)
  28. {
  29. System.out.println(code + ": I am the run method in SubClass.");
  30. }
  31. }
  32.  
  33. public class OverridingAndHidingTest
  34. {
  35. public static void main(String[] args)
  36. {
  37. SuperClass super1 = new SuperClass();
  38. System.out.println("SuperClass super1 = new SuperClass()");
  39.  
  40. super1.run("super1.run()");
  41. // "I am the run method in SuperClass."
  42. super1.runStatic("super1.runStatic()");
  43. // "I am the runStatic method in SuperClass."
  44. // These are obvious.
  45. System.out.println();
  46.  
  47. SuperClass sub1 = new SubClass();
  48. System.out.println("SuperClass sub1 = new SubClass()");
  49.  
  50. sub1.run("sub1.run()");
  51. // "I am the run method in SubClass."
  52. // As you know, the run method in the SubClass object in overridden.
  53.  
  54. sub1.runStatic("sub1.runStatic()");
  55. // "I am the runStatic method in SuperClass."
  56. // The type of sub1 variable is SuperClass so it calls the method in the
  57. // SuperClass not in the SubClass which means hiding works only when it's
  58. // called using ClassName.staticMethod().
  59. System.out.println();
  60.  
  61. SubClass sub2 = new SubClass();
  62. System.out.println("SubClass sub2 = new SubClass()");
  63.  
  64. sub2.run("sub2.run()");
  65. // "I am the run method in SubClass."
  66. // sub2 has an instance of SubClass so it executes the one in the SubClass.
  67.  
  68. sub2.runStatic("sub2.runStatic()");
  69. // "I am the runStatic method in SubClass."
  70. // The type of the variable, sub2 is SubClass so calling runStatic()
  71. // executes the class method, runStatic, in the SubClass.
  72. System.out.println();
  73.  
  74. SuperClass super2 = sub2;
  75. System.out.println("SuperClass super2 = sub2");
  76. // Now reassign an instance of SubClass (more precisely the reference to an
  77. // instance of SubClass) to the variable super2 the type of which is
  78. // SuperClass.
  79. super2.run("super2.run()");
  80. // "I am the run method in SubClass."
  81. // Although the type of super2 variable is SuperClass, it contains a
  82. // reference to an instance of SubClass so it executes the overridden run()
  83. // method in the SubClass.
  84. super2.runStatic("super2.runStatic()");
  85. // "I am the runStatic method in SuperClass."
  86. // The type of super2 is SuperClass and the static methods belong to the
  87. // class but not instance so it doesn't care about the instance and calls
  88. // the runStatic method in the class that is SuperClass.
  89. System.out.println();
  90.  
  91. SubClass sub3 = (SubClass) super2;
  92. System.out.println("SubClass sub3 = (SubClass) super2");
  93. sub3.run("sub3.run()");
  94. // "I am the run method in SubClass."
  95. sub3.runStatic("sub3.runStatic()");
  96. // "I am the runStatic method in SubClass."
  97. // Reassigning it to SubClass type variable makes it use the runStatic
  98. // method in the SubClass.
  99. System.out.println();
  100.  
  101. SubClass.runStatic("SubClass.runStatic()");
  102. // "I am the runStatic method in SubClass."
  103. // SubClass.runStatic() hides SuperClass.runStatic()
  104. SubClass.runStatic2("SubClass.runStatic2()");
  105. // "I am the runStatic2 method in SuperClass."
  106. System.out.println();
  107.  
  108. SuperClass.runStatic("SuperClass.runStatic()");
  109. // "I am the runStatic method in SuperClass."
  110. SuperClass.runStatic2("SuperClass.runStatic2()");
  111. // "I am the runStatic2 method in SuperClass."
  112. }
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement