Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Classes: Abstract And Interface

 
sanju
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the basic difference between Abstract class and Implementing an Interface class. As per OOPs Inheritance concept, Abstract class supports inheritance but Interface class doesn't. Abstract class have single super class, but Interface class can implement many classes. Suppose I'm implementing two classes A & B to C. A & B have method FF with same name and same signature, then how class C is going to implement method FF i.e. which class method will be called (whether A or B). And how its going to support the funda of inheritance here?
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java does not support multi-class inheritance so it isn't an issue.
You said:
suppose I'm implementing two classes A & B to C.

So we are doing this:
public class C implements A, B
Notice that A and B are interfaces.
You then say:
A & B have method FF with same name and same signature, then how class C is going to implement method FF. i.e. which class method will be called (whether A or B).

Since all the methods of an interface are abstract, it doesn't really matter! If A and B both have method FF, and method FF is abstract (which it has to be) then when class C implements FF, it doesn't really matter which FF it implements since they will be the same (assuming that the FF the two interfaces define has the same parameter list).

So which FF did C implement in the example above?
You also say this:
Interface class can implement many classes

This isn't true. An interface can extend many interfaces.
public interface X extends Y, Z
where Y and Z are both interfaces.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
To clarify
I quote from JLS in 9.4.1
http://java.sun.com/docs/books/jls/html/9.doc.html#40596

"It is possible for an interface to inherit more than one method with the same signature (�8.4.2). Such a situation does not in itself cause a compile-time error. The interface is considered to inherit all the methods. However, a compile-time error occurs if, for any two such inherited methods, either they have different return types or one has a return type and the other is void. (The throws clauses do not cause errors in this case.)"
but note this is true only for methods not variables inherited from interfaces
i quote again for variables
9.3.2.1 Ambiguous Inherited Fields
If two fields with the same name are inherited by an interface because, for example, two of its direct superinterfaces declare fields with that name, then a single ambiguous member results. Any use of this ambiguous member will result in a compile-time error
Hope this helps. Any doubt please feel free to write.
Rahul
 
Thillai Sakthi
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by rahul_mkar:
hi,
To clarify
I quote from JLS in 9.4.1
http://java.sun.com/docs/books/jls/html/9.doc.html#40596

"It is possible for an interface to inherit more than one method with the same signature (�8.4.2). Such a situation does not in itself cause a compile-time error. The interface is considered to inherit all the methods. However, a compile-time error occurs if, for any two such inherited methods, either they have different return types or one has a return type and the other is void. (The throws clauses do not cause errors in this case.)"
but note this is true only for methods not variables inherited from interfaces
i quote again for variables
9.3.2.1 Ambiguous Inherited Fields
If two fields with the same name are inherited by an interface because, for example, two of its direct superinterfaces declare fields with that name, then a single ambiguous member results. Any use of this ambiguous member will result in a compile-time error
Hope this helps. Any doubt please feel free to write.
Rahul

 
Thillai Sakthi
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your Quote:
9.3.2.1 Ambiguous Inherited Fields
If two fields with the same name are inherited by an interface because, for example, two of its direct superinterfaces declare fields with that name, then a single ambiguous member results. Any use of this ambiguous member will result in a compile-time error
Here's my version:
All the variables in an interface are by default "final" and "static". Hence once you have declared a variable as final and initialized it in one of the interfaces how one can define another variable of the same name in another variable(it defeats the purpose of the variable being a final variable). Hence you get a compile time error. Out of interface, even if you try in ant other methods the same funda works.
Hope it is an justifiable explanation to the above text. If I am wrong pls correct me.
Thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic