简体   繁体   中英

Why is an inner class forced to implement its interface methods, even if the outer class has it?

public class Outer{
  public void sayHello(){ System.out.println("Hello!");}
  public class Inner implements HelloSayers{}
public interface HelloSayers{
  public void sayHello();
}

The type Outer.Inner must implement the inherited abstract method HelloSayers.sayHello().

But the problem is the inner class should be considered as implementer of the outer methods. Am I wrong?

Yes, you are wrong.

Inner can access the members of Outer , but that does not mean it shares those members.

Ie every instance of Inner contains a reference to the corresponding Outer object ( Outer.this ). If you access a member of Outer from Inner in your java code, the compiler just translates this to a access to the member of the Outer referenced by the Inner object. The Inner class does not contain those members.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM