2

In java can inner classes inherit from an abstract class defined outside of the inner class's outer class?

Also can abstract classes implement constructors?

Jens Schauder
  • 77,657
  • 34
  • 181
  • 348
David
  • 14,569
  • 34
  • 78
  • 107

2 Answers2

9

Yes to both.

For example it is quite common to extend Swing Listener Adapter classes in inner classes

Why didn't you just try it?

Jens Schauder
  • 77,657
  • 34
  • 181
  • 348
  • I didnt just try becuase right now i'm making a UML diagram for what i'm going to build and its really complicated and detailed so it was easeir to ask becuase i'm not really foccused on implimentation right now and i'm on a tight scedual. – David May 09 '11 at 06:32
  • Ok. I'm not going to comment what I think of this kind of UML diagrams. – Jens Schauder May 09 '11 at 06:37
  • No please do, i'm new to the whole making UML diagrams thing and so if i'm totaly f**king it up i'd like to know. – David May 09 '11 at 07:13
  • Since you asked for it: I think these upfront diagrams a pretty useless. As u experienced you might draw things that just don't work, without realizing it. Why not just write the code? UML is great for documenting and discussing things. If you really want to specify things use code, possibly in the form of tests. – Jens Schauder May 09 '11 at 07:23
0

Do you mean...

abstract public class Outer { 
 public Outer() { System.out.println("Outer.ctor"); }
 abstract public String foo(); 
}

public class Inner {
  public static class Inner2 extends Outer {
    public Inner2() { super(); }
    public String foo() { return "Inner2.foo";}
  }

  public static void main(String[] args) {
    System.out.println("main: " + new Inner2().foo());
  }
}

...?

$ javac -d .  Outer.java Inner.java

$ java -cp . Inner
Outer.ctor
main: Inner2.foo

(The superfluous calls to "super()" are merely illustrative, & are not necessary.)

michael
  • 9,161
  • 2
  • 52
  • 49