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:
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:
Mother
as interface
, then your Embryo
's ecluse
method cannot virtually invoke bear
because it's statically-scopedMother
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
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With