Class A{Public void meth(){}}class B extends A{ Public void meth(){ } public static void main(String args[]){ A a=new A(); a.meth();//which method will this call } }if u want to call a method of class B then how can u achieve this?

This question is related to Oracle Interview

Showing Answers 1 - 9 of 9 Answers

chandra

  • Jun 19th, 2005
 

public static void main(String args[]){ 
A a=new B(); 
a.meth();//which method will this call 
}  
}

  Was this answer useful?  Yes

sarala

  • Oct 25th, 2005
 

The following method ,creating the object of B that refers to A will call the meth() in Class B.

Question: Class A{
Public void meth(){
}
}

class B extends A{
Public void meth(){
}
public static void main(String args[]){
A a=new B();
a.meth();
}
}

Whenever a subclass needs to refer to its immediate superclass, it can do so by use of the keyword ?super? .

  Was this answer useful?  Yes

Ankur

  • Apr 15th, 2006
 

No if you want to really call a method in Derived Class then it is only possible if that method will only be present in Derived Class otherwise you can create direct object of the Derived Class.

  Was this answer useful?  Yes

bhuvanamsk

  • Nov 23rd, 2010
 

To access a method of derived class create object for class B.


B b=new B();
b.meth();


If we access class A 's method meth() will not be called.Subclass B
overrides super class.
to access super class method use the keyword super.


Class A{
Public void meth(){
}
}
class B extends A{
Public void meth(){
super.meth()//to access super class method from subclass (bcoz super and sub
class has the same signature or same method name.
}
public static void main(String args[]){
B b=new B();
b.meth(); both methods in A and B are accessed.
}
}


  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions