Method Overriding

  1. class Base
  2. {
  3. void show()
  4. {
  5. {
  6. System.out.println("method of class Base");
  7. }
  8. }
  9. }
  10. class Son extends Base
  11. {
  12. void show()
  13. {
  14. System.out.println("method of class Son");
  15. }
  16. void access()
  17. {
  18. super.show();
  19. }
  20. public static void main (String ...x)
  21. {
  22. Son s1 =new Son();
  23. s1.access();
  24. }
  25. }
  26. Output:
  27. method of class Base
  1. class Dada
  2. {
  3. void show()
  4. {
  5. {
  6. System.out.println("Dada class");
  7. }
  8. }
  9. }
  10. class Pita extends Dada
  11. {
  12. void show()
  13. {
  14. System.out.println("Pita class");
  15. }
  16. void display2()
  17. {
  18. super.show();
  19. }
  20. }
  21. class Beta extends Pita
  22. {
  23. void display1()
  24. {
  25. super.show();
  26. }
  27. void show()
  28. {
  29. System.out.println("Beta class");}
  30. public static void main (String ...x)
  31. {
  32. Beta b1 =new Beta();
  33. b1.show();
  34. b1.display1();
  35. b1.display2();
  36. }
  37. }
  38. Output:
  39. Beta class
  40. Pita class
  41. Dada class
  1. class Mn
  2. {
  3. void show()
  4. {
  5. System.out.println("Show Mn");
  6. }
  7. }
  8. class Op extends Mn
  9. {
  10. void show()
  11. {
  12. System.out.println("Show Op");
  13. }
  14. public static void main(String...a)
  15. {
  16. Op p1=new Op();
  17. p1.show();
  18. }
  19. }
  20. Output:
  21. Show Op
  1. class Father
  2. {
  3. private void show()
  4. {
  5. System.out.println("method of Father");
  6. }
  7. }
  8. class Son extends Father
  9. {
  10. protected void show()
  11. {
  12. System.out.println("method of Son");
  13. }
  14. public static void main (String ...a)
  15. {
  16. new Son().show();
  17. }
  18. }
  19. Output:
  20. Exception in thread "main" java.lang.NullPointerException at Son.show(Father.java:12) at Son.main(Father.java:16)
  1. class Father
  2. {
  3. void show()throws ArithmeticException
  4. {
  5. System.out.println("method of Father");
  6. }
  7. }
  8. class Son extends Father
  9. {
  10. void show()throws ArithmeticException
  11. {
  12. System.out.println("method of Son");
  13. }
  14. public static void main (String ...a)
  15. {
  16. new Son().show();
  17. }
  18. }

| Copyright ©2016 | All Rights Reserved |
| Design by Uves Khan |

Free Web Hosting