File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
ramchander yshetti
Ranch Hand
Posts: 97
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An interface has no implementation of the method, only definition.
So the abstract class's method will be called
 
Shaan Shar
Ranch Hand
Posts: 1249
Java Spring Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1249
Java Spring Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 31
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Christophe Verré
Sheriff
Pie
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic