继承嵌套内部类的Java方法,其中内部类具有私有标识符。

huangapple go评论67阅读模式
英文:

java inherit method of Nester Inner class where Inner class has private Identifier

问题

Here's the translated code portion:

public class Testing {

  public String name = null;

  public String getName() {
    TestingInner test = new TestingInner("Testing inner override");
    return test.getNameString();
  }

  public String testName() {
    return this.name;
  }

  private class TestingInner {

    String nameString = null;

    public TestingInner(String name) {
      this.nameString = name;
    }

    public String getNameString() {
      return "Inner Class " + this.nameString;
    }
  }
}

public class Testing2 extends Testing.TestingInner {

  @Override
  public String getNameString() {
    System.out.println("Override Inner class method");
    return "This is";
  }
}

Regarding your question, the getNameString() method in the inner class TestingInner is private. In Java, private methods cannot be overridden by subclasses, including inner classes. If you want to override this method, you would need to make it at least package-private (default access) or higher, such as protected or public, in the TestingInner class.

英文:
public class Testing {

  public String name = null;

  public String getName() {
    TestingInner test = new TestingInner("Testing inner override");
    return test.getNameString();
  }

  public String testName() {
    return this.name;
  }

  private class TestingInner {

    String nameString = null;

    public TestingInner(String name) {
      this.nameString = name;
    }

    public String getNameString() {
      return "Inner Class " + this.nameString;
    }
  }
}

public class Testing2 extends Testing.TestingInner {

  @Override
  public String getNameString() {
    System.out.println("Override Inner class method");
    return "This is";
  }
}

Getting multiple error message which is expected

> The method getNameString() of type Testing2 must override or implement a supertype method
>
> The type Testing.TestingInner is not visible

Question is there any way to override getNameString() which is under inner class which is private.
Changing private to public will work, but wanted to see if any implementation exist to override getNameString()

答案1

得分: 3

以下是已经翻译好的部分:

要能够扩展一个类,该类必须是可访问的:参考JLS 8.1.4

ClassExtends:
extends ClassType

...

ClassType必须指代一个可访问的类(§6.6),否则将发生编译时错误。

参考JLS 6.6.1,"确定可访问性"(重点添加):

...

  • 否则,该成员(类、接口、字段或方法)或构造函数被声明为私有。只有在以下情况下才允许访问:
    • 访问发生在封闭该成员或构造函数声明的顶层类或接口的主体内。
    • 访问发生在封闭该成员声明的顶层类或接口的许可条款中。
    • 访问发生在封闭该成员声明的顶层记录类的记录组件列表中。

您的类将在Testing的主体内可访问,这是顶层类,因此您可以在那里扩展它。但在Testing的主体之外,您不能这样做。

英文:

In order to be able to extend a class, the class must be accessible: referring to JLS 8.1.4:

> ClassExtends:
> extends ClassType
>
> ...
>
> The ClassType must name an accessible class (§6.6), or a compile-time error occurs.

Referring to JLS 6.6.1, "Determining Accessibility" (emphasis added):

> ...
> * Otherwise, the member [class, interface, field, or method] or constructor is declared private. Access is permitted only when one of the following is true:
> * Access occurs from within the body of the top level class or interface that encloses the declaration of the member or constructor.
> * Access occurs in the permits clause of the top level class or interface that encloses the declaration of the member.
> * Access occurs in the record component list of the top level record class that encloses the declaration of the member.

Your class would be accessible inside the body of Testing, the top-level class, so you could extend it there. But you can't do it outside the body of Testing.

答案2

得分: 1

不行,你不能这样做。你不能扩展一个不可见的类,因此没有可见的内容可以重写。它是一个内部类是无关紧要的。

英文:

No, you can't do it. You can't extend a class that isn't visible so there is nothing visible to override. That it is an inner class is irrelevant.

huangapple
  • 本文由 发表于 2023年6月13日 05:11:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76460361.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定