wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Classes: Abstract And Interface Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Classes: Abstract And Interface" Watch "Classes: Abstract And Interface" New topic
Author

Classes: Abstract And Interface

sanju
Greenhorn

Joined: Jun 05, 2000
Posts: 2
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

Joined: May 05, 2000
Posts: 13974
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.

Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
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

Joined: Jun 17, 2000
Posts: 102
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


Regards,
Shakthi
Thillai Sakthi
Ranch Hand

Joined: Jun 17, 2000
Posts: 102
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Classes: Abstract And Interface
 
Similar Threads
Requirement of Interface in Java
Interview Question: interface vs abstract class
extends vs implements
Abstract & interace
why we use interface instead of abstract class