Why "extends" precedes "implements" in class declaration [closed]
Asked Answered
O

2

53

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.

Oilla answered 10/5, 2012 at 16:5 Comment(8)
Because that's how Java is.Ariellearies
...because that's the way the lexer mandates it?Kinnie
Out of curiosity, why does the order matter to you? Are you trying to achieve something by having another order?Outlet
@NathanielFord New word order. :)Mientao
I think we have to ask why James Gosling did so. =)Ascanius
@Mientao *new word order.Veloz
@Mientao Why do we deserve this punishment? ;)Outlet
@Izjun Be sure to clarify if the answers aren't helpful... and also accept an answer if it is!Outlet
O
108

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.

Outlet answered 10/5, 2012 at 16:13 Comment(7)
I do my best. After all, we're all in this together!Outlet
@NathanielFord, are we really? I've been grossly misinformed.Wellmeaning
If don't follow the rules in a class declaration a compile-time error occurs.Ascanius
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.Militia
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.Duala
This has changed with KotlinBancroft
@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?Outlet
V
5

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; }.

Veloz answered 10/5, 2012 at 16:7 Comment(2)
Something of C. As code is read more than written verbosity has benefits.Ascanius
It is not a convention. It is a syntax rule. Conventions you can ignore, rules are enforced by the compiler.Scarabaeus

© 2022 - 2024 — McMap. All rights reserved.