• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

java interface methods cannot have body?

 
Ranch Hand
Posts: 150
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

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

http://download.oracle.com/javase/6/docs/api/java/util/SortedMap.html

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?

Thanks!
 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 src.zip which comes with your JDK installation.

Interfaces cannot have method implementations.
 
Greenhorn
Posts: 13
Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 150
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?



 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
And then the flying monkeys attacked. My only defense was this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic