45 lines
1.0 KiB
Java
Raw Normal View History

2015-09-07 11:44:36 -06:00
// innerclasses/BigEgg2.java
2016-12-30 17:23:13 -08:00
// (c)2017 MindView LLC: see Copyright.txt
2015-11-15 15:51:35 -08:00
// We make no guarantees that this code is fit for any purpose.
2016-09-23 13:23:35 -06:00
// Visit http://OnJava8.com for more book information.
2016-01-25 18:05:55 -08:00
// Proper inheritance of an inner class
2015-06-15 17:47:35 -07:00
class Egg2 {
protected class Yolk {
2015-12-02 09:20:27 -08:00
public Yolk() {
System.out.println("Egg2.Yolk()");
}
public void f() {
System.out.println("Egg2.Yolk.f()");
}
2015-06-15 17:47:35 -07:00
}
private Yolk y = new Yolk();
2015-11-03 12:00:44 -08:00
public Egg2() { System.out.println("New Egg2()"); }
2015-06-15 17:47:35 -07:00
public void insertYolk(Yolk yy) { y = yy; }
public void g() { y.f(); }
}
public class BigEgg2 extends Egg2 {
public class Yolk extends Egg2.Yolk {
2015-12-02 09:20:27 -08:00
public Yolk() {
System.out.println("BigEgg2.Yolk()");
}
2015-06-15 17:47:35 -07:00
@Override
2015-12-02 09:20:27 -08:00
public void f() {
System.out.println("BigEgg2.Yolk.f()");
}
2015-06-15 17:47:35 -07:00
}
public BigEgg2() { insertYolk(new Yolk()); }
public static void main(String[] args) {
Egg2 e2 = new BigEgg2();
e2.g();
}
2015-09-07 11:44:36 -06:00
}
/* Output:
2015-06-15 17:47:35 -07:00
Egg2.Yolk()
New Egg2()
Egg2.Yolk()
BigEgg2.Yolk()
BigEgg2.Yolk.f()
2015-09-07 11:44:36 -06:00
*/