GeeCON Prague 2014*
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
Author

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 ("----"); }
}
TIA
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
Consultant @ Xebia. Sonny Gill Tweets
 
GeeCON Prague 2014
 
subject: Problems in extending interface