File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Problems in extending interface Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Problems in extending interface" Watch "Problems in extending interface" New topic

Problems in extending interface

Nelson Nadal
Ranch Hand

Joined: Jun 06, 2002
Posts: 170
I am extending an existing Interface and am getting the compile error
'Interface method cannot have body'
How do I add a method to an existing interface without having to implement every single method?
My attempt so far is to start with...
public interface NewUser extends User {
public String newMethod () { return ("----"); }
Sonny Gill
Ranch Hand

Joined: Feb 02, 2002
Posts: 1211

You have got some basic concepts mixed up there, mate.
An interface provide method signatures, then some class implements the interface by providing method bodies for those methods.
Try to find some stuff on the benefits of separtating interface from implementation to understand why it is so.
What you may want to do is -
extend the interface as
public interface NewUser extends User{
public String newMethod();
This adds one more method to those specified by the User interface that the implementation class has to implement. For example,
public class NewUserImpl implements NewUser{
public String newMethod (){
return ("----");
//Implementation of other methods specified by User interface

The future is here. It's just not evenly distributed yet. - William Gibson
Sonny Gill LinkedIn
I agree. Here's the link:
subject: Problems in extending interface
It's not a secret anymore!