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 will help you in learning.Please leave your comments and suggestions in comment section. if you any doubts please use search box provided right side. Search there for answers thank you.
«
Next
Newer Post
»
Previous
Older Post

1 comments for Method overriding example program

Select Menu