简体   繁体   中英

Does a class inherit an abstract inner class declared within an interface when the class implements that interface?

I am running the following java version
openjdk version "10.0.1" 2018-04-17
OpenJDK Runtime Environment (build 10.0.1+10-Ubuntu-3ubuntu1)
OpenJDK 64-Bit Server VM (build 10.0.1+10-Ubuntu-3ubuntu1, mixed mode)

Program 1

class Outer{
abstract class Inner{
    void m1(){
        System.out.println("Inner class m1 method");
    }
  }

 }


class Outer2 extends Outer.Inner{
Outer2(){
new Outer().super();
  }
}
class Test{
public static void main(String[] args) {
new Outer2().m1();
 }
}

input: deep (master *) JavaStuffs $ javac sample.java
deep (master *) JavaStuffs $ java Test
output: Inner class m1 method

Program 2

interface Outer{
    abstract class Inner{
    void m1(){
        System.out.println("Inner class m1 method");
    }
  }
}
class J implements Outer{

}
class Outer2 extends J.Inner{

Outer2(){
    new J().super();
  }
}
class Test{
public static void main(String[] args) {
    new Outer2().m1();

  }
}

input: deep (master *) JavaStuffs $ javac sample.java
output: sample.java:1309: error: illegal qualifier; Inner is not an inner class
new J().super();
^ 1 error

Program 3

interface Outer{
abstract class Inner{
    void m1(){
        System.out.println("Inner class m1 method");
   }
 }
}
class J implements Outer{
class InnerJ extends Inner{

 }
}
class Outer2 extends J.Inner{
Outer2(){
new J().super();
 }
}
class Test{
public static void main(String[] args) {
    new J().new InnerJ().m1();
    new Outer2().m1();
 }
}

input: deep (master *) JavaStuffs $ javac sample.java
output: sample.java:1372: error: illegal qualifier; Inner is not an inner class
new J().super();
^ 1 error

Here in Program 2 and Program 3 I expected that the class will inherit the inner abstract class when it implements the interface. But it's not happening so! Can anyone explain? A precise reason will be highly appreciated!

A member class defined inside an interface is always implicitly static . It's not an inner class, but a static member class, and it doesn't have an outer class instance associated with it.

The Java Language Specification states this in section 9.5 :

A member type declaration in an interface is implicitly public and static. It is permitted to redundantly specify either or both of these modifiers.

Hence, in your program 2, inside the constructor of class Outer2 , you cannot say new J().super(); , because Outer2 doesn't have an outer instance - it is not an inner class. The error from javac says as much: "illegal qualifier; Inner is not an inner class"

No. Classes inherit instance variables and instance methods from their parents, and they implement the same interfaces. They can access static members of their parent classes and directly or indirectly implemented interfaces as if they were inherited, but that's technically a different story.

Nested classes and interfaces are not inherited, neither from parent classes nor from implemented interfaces, but an instance of a child class can serve as the containing instance of its parent class's inner class.

Nesting a class or interface inside another provides first and foremost an additional level of namespacing. Parent.Inner is a different class from Child.Inner -- without considering any other factor, you know because their names are different. Child classes do not, therefore, inherit nested classes. There's no good way to do it, and no particular advantage either.

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