Why "extends" precedes "implements" in class declaration

63,254

Solution 1

When the Java compiler turns a class into bytecode, it must first look to a parent class. That is because the underlying implementation of classes is to point to the bytecode of the parent class - which holds the relevant methods and fields. Then it adds in pointers to the code of the child class functions - some of which are mandated by the 'implements' keyword.

Because the parent class must be compilable, it is easier if the compiler knows up front what that class is. Further, you can extend only one class but implement any number of interfaces. The compilation time climbs if the extends keyword can be intermingled amongst any number of implements instructions. Compilers want to fail as fast as possible to decrease dev time, so this choice is logical. Further, it helps you think clearly about the class for the same reason.

Solution 2

Probably to make the compiler's job easier. It's just a convention. There isn't any advantage to being able to rearrange these things.

It's like asking why Java functions aren't written in pre-order notation like public int (int a, int b)add{ return a+b; }.

Share:
63,254
lzjun
Author by

lzjun

Updated on July 09, 2022

Comments

  • lzjun
    lzjun almost 2 years

    Why must implement always be written after extend in a class declaration? For example:

    public class Register extends ActionSupport implements ModelDriven
    

    Why can it not be:

    public class Register implements ModelDriven extends ActionSupport 
    

    The latter produces a compile-time error.

  • weltraumpirat
    weltraumpirat about 12 years
    +1 Finally a sane answer. Thank you. :)
  • Nathaniel Ford
    Nathaniel Ford about 12 years
    I do my best. After all, we're all in this together!
  • jn1kk
    jn1kk about 12 years
    @NathanielFord, are we really? I've been grossly misinformed.
  • Paul Vargas
    Paul Vargas about 12 years
    If don't follow the rules in a class declaration a compile-time error occurs.
  • Paul Vargas
    Paul Vargas about 12 years
    Something of C. As code is read more than written verbosity has benefits.
  • Stephen C
    Stephen C over 6 years
    It is not a convention. It is a syntax rule. Conventions you can ignore, rules are enforced by the compiler.
  • Will
    Will over 5 years
    Goodness, I am glad to find that someone gave a concrete answer instead of rubbish like "Because that's how Java is", "...because that's the way the lexer mandates it?". -_- Only detracting from the community.
  • Akhilesh Dhar Dubey
    Akhilesh Dhar Dubey over 3 years
    I think this is also done because we can implement multiple interfaces, So it is easy to update any new interface implementation at the end which looks clean.
  • Gilbert
    Gilbert over 3 years
    This has changed with Kotlin
  • Nathaniel Ford
    Nathaniel Ford over 3 years
    @Ssenyonjo I'm not entirely sure what you mean by 'this', but the class declaration for interfaces is pretty different in Kotlin. While it's also a JVM language, I'm not sure that that fact really applies to the question being asked here?