permaculture playing cards
The moose likes Beginning Java and the fly likes java interface methods cannot have body? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "java interface methods cannot have body?" Watch "java interface methods cannot have body?" New topic

java interface methods cannot have body?

Lou Pelagalli
Ranch Hand

Joined: Nov 11, 2003
Posts: 150

I get the error "java interface methods cannot have body" but when I look at interfaces like java.util.SortedMap

is see methods declarations that appear to have a body because of the statements in the method summary, for example a Comparator is returned from comparator().

This seems like a contradiction to me, can someone please help clarify?

Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3152

The documentation contains the information of what the method should be doing when its implemented. Its a guideline for the implementing class.
On the other hand you should be checking out the source code of SortedMap to find out how the interface is designed. You can get the source code in the which comes with your JDK installation.

Interfaces cannot have method implementations.

Mohamed Sanaulla | My Blog
Eduardo Hernandez

Joined: Jul 09, 2011
Posts: 13

You need to understand this:

Method body or method implementation is what is inside curly braces after the method declaration (including the braces).
Return type could be a primitive or an object reference.

Interfaces declaration consist of abstract methods declarations plus constant(s) if any. What does it means to declare a method to be abstract? The method must provide a signature (name and 0 or more parameters type), return type and throws if any (optional) and no method body (not implemented) (can't end with curly braces but a semicolon after the "closing parenthesis"): myMethod() or myMethod(int x) are valid method signature examples but then you MUST add the return type: int myMethod(); or void myMethod(int x);

One last thing to remember interface methods are implicitly public and abstract.

What you're seeing is a method named comparator that happens to return a Comparator.
Lou Pelagalli
Ranch Hand

Joined: Nov 11, 2003
Posts: 150
Thank you Mohamed & Eduardo! That really clears this up for me!

I reviewed the code in SortedMap as Mohamed suggested, it was very helpful.

To expand on this, consider
SortedMap sortedMap = new TreeMap();
TreeMap treeMap = new TreeMap();
Object obj = new TreeMap();

Can be described generically as
Implementation name = new ClassName();

1) sortedMap would contain all of the methods and variables of a SortedMap, but none of the TreeMap that were not implemented to satisfy the SortedMap interface.

2) treeMap would contain all of the methods and variables of a TreeMap, including those of a SortedMap because TreeMap must satisfy the implementation of a SortedMap.

3) obj would contain all of the methods and variables of an Object, but none of the TreeMap or SortedMap.

So I could do something like - the syntax may not be exactly correct.
public interface Shipment {
public String getShippingInstructions();

public class Order implements Shippment{
public String getShippingInstructions() {
return "Ship by slow boat";

public class Gift implements Shipment {
public String getShippingInstructions() {
return "Ship by rocket and attach thank you card";


Shipment shipment1 = new Order();
shipment1.getShippingInstructions() returns "Ship by slow boat"

Shipment shipment2 = new Gift();
shipment2.getShippingInstructions() returns "Ship by rocket and attach thank you card"

Object shipment3 = new Gift();
shipment3.getShippingInstructions() won't compile because shipment3 does not contain method getShippingInstructions().

So the seemingly different Gift and Order objects can both go through the same shipping process.

One use of polymorphism.

Am I close? Way off?

Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 15080

It's good practice to program to an interface, not an implementation.

For example, when you need a list in your application to store items, declare it like this:

Do not declare it like this:

When you use the first way, it is very easy to switch to another implementation later, because the rest of the application doesn't know what exact implementation of interface List is being used; it only knows (and it only needs to know) that it's a List. For example, you could decide to use a LinkedList instead of an ArrayList:

To the rest of the application, names would still look like a List.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
I agree. Here's the link:
subject: java interface methods cannot have body?
jQuery in Action, 3rd edition