8/11/2007

Java class extends clause comes before implements clause

The following class failed to compile with either JDK 5 or 6:

import java.io.Serializable;

public class ImplementsExtendsOrder implements Serializable extends Object {
}

\jdk6\bin\javac ImplementsExtendsOrder.java
ImplementsExtendsOrder.java:7: '{' expected
public class ImplementsExtendsOrder implements Serializable extends Object {
^
1 error
It turned out when declaring a java class, the extends clause has to come before implements clause. The error message about missing {did lead me to check matching {} many times without finding anything.

The correct way to declare such a class is to put implements clause before extends clause:
import java.io.Serializable;

public class ImplementsExtendsOrder extends Object implements Serializable {
}
Tags: ,

8 comments:

Anonymous said...

urhm. not according to my compiler, nor the Java Language Specification.

kalyan said...

It should be

public class ImplementsExtendsOrder extends Object implements Serializable {

}

admin said...

If you can compile it. which compiler (vendor, version) do you use?

I tried Sun's JDK javac on Windows and Linux, and both failed with the same result.

IBM Eclipse compiler gave the same error, too:

/usr/bin/javac -version
Eclipse Java Compiler 0.319_R21x, Copyright IBM Corp 2000-2003. All rights reserved.

/usr/bin/javac Foo.java
----------
1. ERROR in Foo.java (at line 1)
public class Foo implements java.io.Serializable extends Object
^^^^^^^
Syntax error on keyword "extends"; "{" expected
----------
1 problem (1 error)

Lepht said...

aaand another one hits the bookmarks. i've been looking for a homegrown howto for ages. cheers

Lepht
http://sapiensanonym.blogspot.com
lepht@trioptimum.com

Anonymous said...

http://download.oracle.com/javase/tutorial/java/javaOO/classdecl.html

Anonymous said...

The "correct way" that you state and the incorrect way are identical...

javahowto said...

The correct code is now fixed. Thanks.

Steve Smith said...

Great and Useful Article.

Online Java Training

Java Online Training India

Java Online Course

Java EE course

Java EE training

Best Recommended books for Spring framework

Java Interview Questions








Java Course in Chennai

Java Online Training India