Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Basic Interface question

 
kalpana Kumar
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Yasith Vidanaarachchi
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic