Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why "extends" precedes "implements" in class declaration [closed]

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.

like image 502
lzjun Avatar asked May 10 '12 16:05

lzjun


People also ask

Why do we need extends keyword over Implements keyword?

So basically, extends keyword is used to extend the functionality of the parent class to the subclass. In Java, multiple inheritances are not allowed due to ambiguity. Therefore, a class can extend only one class to avoid ambiguity. Implements: In Java, the implements keyword is used to implement an interface.

Can we use implements and extends together?

Yes, you can. But you need to declare extends before implements : public class DetailActivity extends AppCompatActivity implements Interface1, Interface2 { // ... } Any number of interfaces can be implemented, if more than one then each needs to be separated with a comma.

Can a class extend and implements?

Differences between extends vs implementsA class can extend only one class; but can implement any number of interfaces. A subclass that extends a superclass may override some of the methods from superclass. A class must implement all the methods from interfaces.

What is the difference between extends and implements?

Difference: implements means you are using the elements of a Java Interface in your class. extends means that you are creating a subclass of the base class you are extending. You can only extend one class in your child class, but you can implement as many interfaces as you would like.


2 Answers

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.

like image 98
Nathaniel Ford Avatar answered Sep 29 '22 12:09

Nathaniel Ford


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

like image 26
trutheality Avatar answered Sep 29 '22 12:09

trutheality