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


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Interfaces" Watch "Interfaces" New topic
Author

Interfaces

Rajat Sarkar
Greenhorn

Joined: Sep 07, 2008
Posts: 18
Can interfaces implement another interface or extend another class?
Omar Al Kababji
Ranch Hand

Joined: Jan 13, 2009
Posts: 357
No interfaces can only extend other interfaces

interface1 extends interface2

this is the only possible thing to do with interfaces.

however abstract classes, can implement interfaces and they can either provide a definition for the interface methods or leave it for the first concrete class that extends that abstract class.


Omar Al Kababji - Electrical & Computer Engineer
[SCJP - 90% - Story] [SCWCD - 94% - Story] [SCBCD - 80% - Story] | My Blog
Stephen Davies
Ranch Hand

Joined: Jul 23, 2008
Posts: 352
omar al kababji wrote:No interfaces can only extend other interfaces

interface1 extends interface2

this is the only possible thing to do with interfaces.

however abstract classes, can implement interfaces and they can either provide a definition for the interface methods or leave it for the first concrete class that extends that abstract class.


Yes with regards to inheritance this is true. However it is not only Abstract Classes which can implement interfaces. Concrete classes themselves are also able to implement interfaces, and in doing so are required to fulfill the contract of the methods declared in the interface.

Additionally interfaces are a fine example of polymorphism if the technique of "programming to an interface" is used. In Java it is strongly recommended that any arguments to a parameter for a method should be as general as possible. This ensures among other things, that there is a good degree of flexibility in your code. What it means in code, is that any object maybe passed to your method providing it implements the required interface, so in the future if you have additional Objects with a varying degree of behaviors and attributes they may still be handled by your Method providing they implement the interface.

For example take the following example (tried and tested in Eclipse);




This should produce the following output:



Hope this of use


be a well encapsulated person, don't expose your privates, unless you public void getWife()!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Interfaces
 
Similar Threads
Framework -- Architecture
Interfaces
B&S: find/search
Inner classes must extend?