-1

I was trying to extend from a class and implement an interface as well.Here is my code.

class A {

    public void print1() {
          System.out.println("Hello Class");
    }
}

interface B {

    public void print2();
}

class C extends A implements B {
    public void print2() {
        System.out.println("Interface");
    }
}

First I did is

class C implements B extends A  {
    public void print2() {
        System.out.println("Interface");
    }
}

And it leads to an error. It seems that extends keyword needs to come before implements . Why it is needed ? Any specific reasons or is it just the way java is developed. ?

MANOJ GOPI
  • 1,279
  • 10
  • 31
prime
  • 14,464
  • 14
  • 99
  • 131
  • 4
    Because that is how the language is designed per the [Java Language Specification, sections 8.1.4 and 8.1.5](http://docs.oracle.com/javase/specs/jls/se8/html/jls-8.html). – Hovercraft Full Of Eels Jan 18 '15 at 14:50

2 Answers2

1

Because you can extend only from one class, but implement several interfaces. I think it's just a matter of good syntax style forced by the language's grammar. It seems like this question is duplicate of this one anyway.

Community
  • 1
  • 1
ale64bit
  • 6,232
  • 3
  • 24
  • 44
1

This is defined in the JLS, section 8.1:

NormalClassDeclaration:
{ClassModifier} class Identifier [TypeParameters] [Superclass] [Superinterfaces] ClassBody

Here you can clearly see that a superclass must be declared before any superinterface declarations.

Keppil
  • 45,603
  • 8
  • 97
  • 119