7

Having this class

public abstract class Mother{
  public class Embryo{
    public void ecluse(){
      bear(this);
    }
  }
  abstract void bear(Embryo e);
}

i can create a Instance of Embryo only if i have a instance of Mother:

new Mother(){...}.new Embryo().ecluse();

Question:

  • How to define Mother as an interface?
Grim
  • 1,938
  • 10
  • 56
  • 123
  • 1
    Not completly unrelated but also not really answering your question: http://stackoverflow.com/questions/2400828/inner-class-within-interface – ctst Jan 28 '16 at 15:15
  • 1
    Could you provide some information on how you plan to use this interface/class? – proskor Jan 28 '16 at 15:15

1 Answers1

6

The nested class Embryo is implicitly static in an interface.

As such, it does not have access to the virtually invokable method bear, which would pertain to instances of your Mother interface.

Therefore:

  • Either you declare Mother as interface, then your Embryo's ecluse method cannot virtually invoke bear because it's statically-scoped
  • Or, you keep Mother as an abstract class, but require an instance of a Mother (anonymous or a child class' instance) in order to get an instance of Embryo (but Embryo is instance-scoped unless specified otherwise, and can invoke bear virtually)

Self-contained example

package test;

public class Main {

    public interface MotherI {
        // this is static!
        public class Embryo {
            public void ecluse() {
                // NOPE, static context, can't access instance context
                // bear(this);
            }
        }
        // implicitly public abstract
        void bear(Embryo e);
    }

    public abstract class MotherA {
        public class Embryo {
            public void ecluse() {
                // ok, within instance context
                bear(this);
            }
        }

        public abstract void bear(Embryo e);
    }

    // instance initializer of Main
    {
        // Idiom for initializing static nested class
        MotherI.Embryo e = new MotherI.Embryo();
        /*
         *  Idiom for initializing instance nested class
         *  Note I also need a new instance of `Main` here,
         *  since I'm in a static context.
         *  Also note anonymous Mother here.
         */
        MotherA.Embryo ee = new MotherA() {public void bear(Embryo e) {/*TODO*/}}
           .new Embryo();
    }

    public static void main(String[] args) throws Exception {
        // nothing to do here
    }
}
Mena
  • 47,782
  • 11
  • 87
  • 106
  • Neither an `abstract class` nor an `interface` can be intstanticated without a class who is implementing `Mother`, you need another class anyway! So why does it care? Instances of Embryo are only available via a instance of the outer class `Mother`, if its a interface or an abstract class, – Grim Jan 28 '16 at 15:20
  • Not exactly for the last part. Let me give you an example. – Mena Jan 28 '16 at 15:23
  • I understand the first sentence compleatly. Why is it so? – Grim Jan 28 '16 at 15:26
  • i did not need the codeexample, i already got what you mean, but why is it so? – Grim Jan 28 '16 at 15:30
  • 1
    @PeterRader I think this is just part of the Java language specifications, but I'm having trouble finding an explicit reference. All member of `interface`s are `static public final` implicitly. However nested `class`es of `interfaces` are only `public static` but not `final`, as they can be extended. – Mena Jan 28 '16 at 15:37