Here I am taking class A:

package com.oops;

class A{
void msg(){
System.out.println("Hello");
}
}

Here I am taking class B:

class B extends A{
void msg(){
System.out.println("Welcome");
}
 
 public static void main(String args[]){
 A a=new A();
         B b=new B();
        A obj=new B();
     
        System.out.println(a.msg());   //   Hello
        System.out.println(obj.msg());  // Welcome
        System.out.println(b.msg());   //Welcome
}
}



Output:
Hello
Welcome
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

No comments

Leave a Reply

Select Menu