extends class and implements interface in java
Asked Answered
U

6

42
interface Bouncable{ } 
interface Colorable extends Bouncable{ } 
class Super implements Colorable{ } 
class Sub extends Super implements Colorable {} // Ok (case -1)

But,

class Sub implements Colorable extends Super {} // error (case -2)

Why case-2 showing compilation error { expected. Why ?? Although, case-1 executes without error.

Umbrage answered 24/12, 2012 at 13:6 Comment(4)
possible duplicate of #10538510Whistle
@Whistle may be. But,you can say, it belongs to the second part of my questionUmbrage
i don't know, why people doing downvote without writing their reason. If anyone have doubts, they will ask, on the other hand, if other knows that answer, then post their answer instead of doing downvote. People are mis-using the down-vote features of SO.Umbrage
And, i really don't think so, it was really irrelevant question. As, a beginner, many people don't about this.Umbrage
W
73

extends should go before implements:

class Sub extends Super implements Colorable {}
Whistle answered 24/12, 2012 at 13:7 Comment(8)
why ?? any logic behind that ??Umbrage
just Java syntax, no logicWhistle
then, also i believe, there would be any logic for this Syntax. If you know that, then please explain. I will appreciate.Umbrage
There have to be some rules. Why can't { and [ be interchanged? Why cant ( and ) be interchanged? Same reason.Psychodrama
If you found any supportive comments in JLS or any where. Please do share.Umbrage
For detail answer see.. #10538510Mattiematting
There is logic. If there are more of interfaces to implement, it would be harder to find a superclass at the end of a line. Consider this: class Sub implements Colorable, Shape, Component, SomeInterface, AnotherInterface extends Super {}.Attachment
Because you can only extend 1 class, but implement many interfaces. more readable.Indian
C
9

I have an example to show why extends precedes implements in class declaration,

inerface :

public interface IParent {
    void getName();
    void getAddress();
    void getMobileNumber();
}

abstract class :

public abstract class Parent {
    public abstract void getAge();
    public void getName(){
        System.out.print("the parent name");
    }
}

Child class :

public class Child extends Parent implements IParent {

    @Override
    public void getAddress() {
       System.out.print(" Child class overrides the Parent class getAddress()");
    }

    @Override
    public void getMobileNumber() {
        System.out.print(" Child class overrides the Parent class getMobileNumber()");
    }

    @Override
    public void getAge() {
        //To change body of implemented methods use File | Settings | File Templates.
    }
}

If you observe there is same method getName() in both interface and in abstract class, where in abstract class the method has the implementation.

When you try to implement, then it's mandatory for a class to override all the abstract methods of an interface and then we are trying to extend the abstract class which has already has an implementation for the method getName().

when you create an instance of a Child class and called the method getName() as

Child child = new Child();
child.getName();

There will a conflict for a child to call which method implementation as there will be two implementation for the same method getName().

To avoid this conflict they made it mandatory to extend first and implement an interface later.

so if an abstract class has the same method as in an interface and if abstract class has implemented the method already then for a child class its not necessary to override that method

Comate answered 30/4, 2014 at 10:38 Comment(0)
T
5

There is a rule in java if want to implement an interface and extend a class we must extend a class first then we implement an interface

interface A{}
class super{}

class sub extends super implements A {}

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.

Tieback answered 13/7, 2017 at 15:43 Comment(3)
could you add any reference to support your answer ?Umbrage
instead writing all this in comment, you should have updated your post. Now, your post has less content than your comment. :DUmbrage
Your Answer is copied from #10538510.Adopted
C
2

The syntax for class definition at JLS Syntax Page is

NormalClassDeclaration: 
    class Identifier [TypeParameters] [extends Type] [implements TypeList] ClassBody

I think that for simplifying the syntax rules they did not make it interchangeable.

For making interchangeable you probably need something like:

NormalClassDeclaration: 
    class Identifier [TypeParameters] [ExtendsImplements] ClassBody

ExtendsImplements:
    [extends Type] [implements TypeList] | [implements TypeList] [extends Type]

Or even worst, you might beed to declare Extends and Implements in order to use OR.

I guess it is not that important that it will worth cluttering the parsing rules.

Carib answered 24/12, 2012 at 13:20 Comment(1)
added reference to JLS Syntax PageCarib
L
1

This is because of a specification in JLS. And there is a certain order of elements when you attempt to declare a class in Java:

  • Modifiers such as public, private, etc.
  • The class name, with the initial letter capitalized by convention.
  • The name of the class's parent (superclass), if any, preceded by the keyword extends. A class can only extend (subclass) one parent.
  • A comma-separated list of interfaces implemented by the class, if any, preceded by the keyword implements. A class can implement more than one interface.
  • The class body, surrounded by braces, { }.

Reference:

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

Lemon answered 24/12, 2012 at 13:13 Comment(1)
You explained about the basic declaration in java. But, from your explanation and the link, it is not clear why extend come first than implements.Umbrage
F
0

You have to do like this. You can extends only one class but you can implement multiple interfaces by comma separately. it would be more reader friendly to have the extend first before the implement

class Sub extends Super implements Colorable
Fortissimo answered 24/12, 2012 at 13:19 Comment(1)
i don't think so, it will reason behind this syntax :/ Please give some reference.Umbrage

© 2022 - 2024 — McMap. All rights reserved.