It's not a secret anymore!*
The moose likes Beginning Java and the fly likes which one is called abstract  or interface.....? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "which one is called abstract  or interface.....?" Watch "which one is called abstract  or interface.....?" New topic
Author

which one is called abstract or interface.....?

ramchander yshetti
Ranch Hand

Joined: Apr 22, 2006
Posts: 97
interface A
{
int add(int a,int b)
}
abstract B
{
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
}
}
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

An interface has no implementation of the method, only definition.
So the abstract class's method will be called


[My Blog]
All roads lead to JavaRanch
Shaan Shar
Ranch Hand

Joined: Dec 27, 2005
Posts: 1249

yathamshetti ramchander,

There was a typo abstract class should be there instead of abstract only.
Second thing is

add method is finally implemented in C class so ofcourse C class's add function will be called.

But the other question you might asked when if you implement the add method in abstract class then what happen .. Answer is again add method of Class C will execute.

and please use Code tags to demonstrate the code snippets.

Corrected Code is:




Hoep it helps you.
Shaan Shar
Ranch Hand

Joined: Dec 27, 2005
Posts: 1249

Are you sure Satou kurinosuke about this:


So the abstract class's method will be called



Please come up for this quote with
the given examples



VarunKumar Mallisetty
Ranch Hand

Joined: Jun 18, 2006
Posts: 31
The method from the class C is called, since its the place where the actual method is implemented. In the actual code presented there is no implementation for the method add(). So when extending the abstract class we have to implement that method. If there is already existing implementation for add() as in the code provided by ankur it may be inherited or overridden by another implementation. In either of the cases, we say that the method in class C is called but not method in Abstract Class or interface.
The advantage of this type of code is that objects of type C can be passed as parameters where the expected type is either of type B or type A since with the implementation of add() method we can say that interface A is implemented.


SCJP 6.0
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

Ankur,
Sorry, when I replied, I didn't even look at class C, because the question was: "abstract or interface, which one is called ?"

If class C implements the method, of course it will be called.
Thanks for pointing this out.
 
wood burning stoves
 
subject: which one is called abstract or interface.....?