fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. class Parent {
  8. void show()
  9. {
  10. System.out.println("Parent's show()");
  11. }
  12. }
  13.  
  14. // Inherited class
  15. class Child extends Parent {
  16. // This method overrides show() of Parent
  17. @Override
  18. void show()
  19. {
  20. System.out.println("Child's show()");
  21. }
  22. }
  23.  
  24. // Driver class
  25. class Main {
  26. public static void main(String[] args)
  27. {
  28. // If a Parent type reference refers
  29. // to a Parent object, then Parent's
  30. // show is called
  31. Parent obj1 = new Parent();
  32. obj1.show();
  33.  
  34. // If a Parent type reference refers
  35. // to a Child object Child's show()
  36. // is called. This is called RUN TIME
  37. // POLYMORPHISM.
  38. Child obj2 = new Child();
  39. obj2.show();
  40. }
  41. }
Success #stdin #stdout 0.08s 35668KB
stdin
Standard input is empty
stdout
Parent's show()
Child's show()