0

In Java, can a class implement an interface nested in one of its sub-classes?

default locale
  • 13,035
  • 13
  • 56
  • 62
  • 3
    Could you provide an example of what are you trying to achieve? – default locale Jan 15 '15 at 07:15
  • Thilo, it is not the sub-class that is implementing the interface; it is the super-class that is implementing the interface and the interface is in the sub-class. – Jack DeLano Jan 15 '15 at 07:22
  • Are you asking if the superclass can already provide the implementation, so that the sub-class can just use it when it declares interface compliance? If so, yes. – Thilo Jan 15 '15 at 07:23
  • " it is not the sub-class that is implementing the interface". Well, all subclasses inherit the interfaces from the superclass. So they cannot avoid also implementing it. – Thilo Jan 15 '15 at 07:24
  • Ok, so a sub-class has an interface. My question is can the sub-class's super-class implement that interface? – Jack DeLano Jan 15 '15 at 07:26

1 Answers1

2

No, the compiler will find a cyclic inheritance. Just try it:

class C implements B.A {} 

class B extends C {
   interface A {}
} 
// won't compile
PeterMmm
  • 24,152
  • 13
  • 73
  • 111