aspose file tools*
The moose likes Beginning Java and the fly likes Basic Interface question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Basic Interface question " Watch "Basic Interface question " New topic
Author

Basic Interface question

kalpana Kumar
Ranch Hand

Joined: Jul 03, 2003
Posts: 65
Hi all,
I want to understand interfaces in detail. I do know that interfaces are mainly used to just specify the method definitions and can be overridden in the subclasses.
My understanding is that interface should always be implemented by the subclass
Eg:- class test implements testinterface{}

But as far as the below is concerned

public interface HttpJspPage( this is an interface)
extends JspPage(this is also an interface)

Can the interface be extended.
Can anyone please explain me this scenario.

Thanks
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Note that interface methods cannot contain implementation because they are implicitly abstract. So, because there is no possibility of method bodies "clashing," multiple interfaces can be implemented by a single class.

With that in mind, consider that an interface cannot "implement" another interface because the methods must remain abstract. But because there is no danger of clashing, interfaces can extend multiple interfaces.
[ December 02, 2004: Message edited by: marc weber ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Yasith Vidanaarachchi
Greenhorn

Joined: Sep 18, 2004
Posts: 10
Hi
If you want to know more about interface, An Interface is same as a class
and it is defaultly abstract so you can't have final interfaces and if
you implement an Interface you must implement all of it's methods too
and for your second question you can extend an Iterface from an other
Interface
cheers
YASITH

[ December 20, 2004: Message edited by: yasith vidanaarachchi ]
[ December 20, 2004: Message edited by: yasith vidanaarachchi ]
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
Originally posted by marc weber:
Note that interface methods cannot contain implementation because they are implicitly abstract. So, because there is no possibility of method bodies "clashing," multiple interfaces can be implemented by a single class.


Not quite true...
Being abstract doesn't mean you can't contain implementation code.
Abstract classes in Java can very well contain implementation code for example.
Interfaces are implicitly pure abstract (a concept which Java doesn't otherwise recognise) which does mean there is no implementation code.


42
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Note that I said interface methods are implicitly abstract. I was not referring to the interface itself (which -- as Jeroen points out -- is a more complex matter).
[ December 20, 2004: Message edited by: marc weber ]
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
The others seem to have covered it pretty well, so I'll just make a correction in terminology.

When you provide a body for an abstract method (either from an interface or abstract class), you are merely implementing the method. Think of implementing an interface where you must implement all of its methods or declare your class abstract.

When you provide a new body for an existing non-abstract method from a superclass, only then are you overriding the method.
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1824

As to the original question, yes, an interface can extend another interface. Classes cannot extend an interface; classes can only implement and interface.

Basically, an extended interface simply includes further methods that must be implemented -- the implementing classes must implement all of the methods (or be declred abstract).


Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Basic Interface question