user2066049
user2066049

Reputation: 1371

Method in protected interface within an abstract class not visible to a subclass

If I have

public abstract class Test {

    public void foo(){}

    protected interface Test1{
        public void bar();
    }

}

public class NewTest extends Test{
}

wondering why bar() isn't visible to NewTest though it extends abstract Test? It requires to have NewTest implements Test.Test1. wondering why is that.

Upvotes: 0

Views: 379

Answers (3)

Anuj Kulkarni
Anuj Kulkarni

Reputation: 2199

You need to implement the Test1 class. You need to use this syntax : Class.InnerClass

Here is the example : http://ideone.com/wr5bON

The Test class is not implementing the Test1 class. foo() will not be visible to anyone else unless you implement the interface.

Upvotes: 1

rgettman
rgettman

Reputation: 178263

The interface Test1 is just an interface, whether it's located with the Test class or standalone. The presence of the interface in Test doesn't mean that Test implements Test1, so NewTest doesn't implement it either, unless you explicitly implement it with implements Test.Test1.

Upvotes: 4

Jon Newmuis
Jon Newmuis

Reputation: 26502

You can make the interface (Test1) static to refer to it without an instance of the parent class (Test).

For example:

public abstract class Test {
  public void foo() {}

  protected static interface Test1 {
    public void bar();
  }
}

public class NewTest extends Test {}

Think of this the same way as you would member variables or methods: you can access static members/methods/inner classes if the class is visible; you need an instance of the class to access them if the static keyword is not used.

Upvotes: 2

Related Questions