fork download
  1. // Base class Person
  2. class Person {
  3.  
  4. // Method that displays the
  5. // role of a person
  6. void role() {
  7. System.out.println("I am a person.");
  8. }
  9. }
  10.  
  11. // Derived class Father that
  12. // overrides the role method
  13. class Father extends Person {
  14.  
  15. // Overridden method to show
  16. // the role of a father
  17. @Override
  18. void role() {
  19. System.out.println("I am a father.");
  20. }
  21. }
  22.  
  23. public class Main {
  24. public static void main(String[] args) {
  25.  
  26. // Creating a reference of type Person
  27. // but initializing it with Father class object
  28. Person p = new Father();
  29.  
  30. // Calling the role method. It calls the
  31. // overridden version in Father class
  32. p.role();
  33. }
  34. }
Success #stdin #stdout 0.13s 54588KB
stdin
45
stdout
I am a father.