aquaballoon

Java - Inheritance (Method Overriding)

Jul 15th, 2013
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.55 KB | None | 0 0
  1. http://kin.naver.com/qna/detail.nhn?d1id=1&dirId=1040201&docId=69740211&qb=7J6Q67CUIOyduO2EsO2OmOydtOyKpCDstpTsg4HtgbTrnpjsiqQ=&enc=utf8&section=kin&rank=4&search_sort=0&spq=0
  2.  
  3. // Regular class inheritance
  4. package com.peter.inheritance;
  5.  
  6. public class Main {
  7.  
  8.     public static void main(String[] args) {
  9.         Son ObSon = new Son();
  10.         ObSon.func();
  11.                 ObSon.father_func();    // inheritance from Father class
  12.        
  13.         Father ObFather = new Father();
  14.         ObFather.func();       
  15.     }
  16. }
  17. ------------------
  18. package com.peter.inheritance;
  19.  
  20. public class Father {
  21.     public void func() {
  22.         System.out.println("Father");
  23.     }
  24.  
  25. public void father_func() {
  26.         System.out.println("Father_Func");
  27.     }
  28.  
  29. }
  30. -------------------
  31. package com.peter.inheritance;
  32.  
  33. public class Son extends Father {
  34.     public void func() {             // Overriding
  35.         System.out.println("Son");
  36.     }
  37. }
  38.  
  39. // Abstract class inheritance
  40. package com.peter.inheritance;
  41.  
  42. public class Main {
  43.  
  44.     public static void main(String[] args) {
  45.         Son ObSon = new Son();
  46.         ObSon.func();
  47.     }
  48. }
  49. -----------------------------
  50. package com.peter.inheritance;
  51.  
  52. abstract class Father {
  53.     abstract void func();
  54. }
  55.  
  56. ----------------------------
  57. package com.peter.inheritance;
  58.  
  59. public class Son extends Father {
  60.     public void func() {           // Overriding not Needed!!
  61.         System.out.println("Son");
  62.     }
  63. }
  64.  
  65.  
  66. // Interface class inheritance
  67. package com.peter.inheritance;
  68.  
  69. public class Main {
  70.  
  71.     public static void main(String[] args) {
  72.         Son ObSon = new Son();
  73.         ObSon.func();
  74.     }
  75. }
  76. -------------------------------
  77. package com.peter.inheritance;
  78.  
  79. public interface Father {
  80.     abstract void func();
  81. }
  82.  
  83. -----------------------------
  84. package com.peter.inheritance;
  85.  
  86. public class Son implements Father {
  87.     public void func() {                  // Overriding Needed!!
  88.         System.out.println("Son");
  89.     }
  90. }
  91.  
  92. // Anonymous Inner Class Inheritance
  93. package com.peter.inheritance;
  94.  
  95. public class Main {
  96.  
  97.     public static void main(String[] args) {
  98.         Son ObSon = new Son(){                         // Inner Class
  99.             public void func(){                    // Method Overriding
  100.                 System.out.println("Main");
  101.             }
  102.         };
  103.        
  104.         ObSon.func();
  105.     }
  106. }
  107. -------------------------------
  108. package com.peter.inheritance;
  109.  
  110. public interface Father {
  111.     abstract void func();
  112. }
  113.  
  114. -----------------------------
  115. package com.peter.inheritance;
  116.  
  117. public class Son implements Father {
  118.     public void func() {                  // Overriding Needed!!
  119.         System.out.println("Son");
  120.     }
  121. }
Advertisement
Add Comment
Please, Sign In to add comment