1. package com.oops; 
  2.  
  3. class A
  4.  
  5. void msg(){
  6.  
  7.  System.out.println("Hello");
  8.  
  9.  
  10. }

  1. class B extends A
  2. {
  3.  
  4.  void msg(){
  5.  
  6. System.out.println("Welcome");
  7.  
  8. }
  9.  
  10. public static void main(String args[])
  11.  
  12.     A a=new A();
  13.     B b=new B();
  14.      A obj=new B(); 
  15.  
  16.    System.out.println(a.msg());   //   Hello
  17.         System.out.println(obj.msg());  // Welcome
  18.         System.out.println(b.msg());   //Welcome
  19.  
  20. }  





Output:
  1. Hello
  2. Welcome 
  3. Welcome



Instance Of Java

We are here to help you learn! Feel free to leave your comments and suggestions in the comment section. If you have any doubts, use the search box on the right to find answers. Thank you! 😊
«
Next
Newer Post
»
Previous
Older Post

1 comments for Method overriding example program

Select Menu