This week's book giveaway is in the General Computing forum. We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line! See this thread for details.
interface A { int add(int a,int b); } abstract class B { abstract int add(int a, int b); }
public class C extends B implements A { public int add(int a, int b) { return a+b; } public static void main(String args[]) { C c12=new C(); c12.add(5,7);//which method is called i.e interface or abstract } }
Technically, the method which gets called is neither of interface A nor of class B, rather it is of class C.
Theoritically, you can consider a method call to be associated with the reference it is associated with. However, at run time, method call is decided based on the actual class of instance the reference is pointing to.
In your example, it is the reference of class C (c12) which is calling the method add(). Hence it is the method of class C which gets called (if you prefer to say so). Had it been , we might say that it is the method of interface A which gets called. Similarily, for calling method of class B, we could have written .
Please note, that no matter which version you use, ultimately it is the method of class C which gets executed. This is evident even in case of method add() not being abstract in class B.
Agreed, please don't post the same question in multiple forums, it creates duplicate conversations and wastes the time of the people trying to help you.
Dave.
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.