55 lines
1.1 KiB
Java
Raw Normal View History

2015-09-07 11:44:36 -06:00
// polymorphism/Sandwich.java
2015-12-15 11:47:04 -08:00
// (c)2016 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.
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
2016-01-25 18:05:55 -08:00
// Order of constructor calls
2016-07-07 15:20:35 -06:00
// {main: polymorphism.Sandwich}
2015-06-15 17:47:35 -07:00
package polymorphism;
class Meal {
2015-11-03 12:00:44 -08:00
Meal() { System.out.println("Meal()"); }
2015-06-15 17:47:35 -07:00
}
class Bread {
2015-11-03 12:00:44 -08:00
Bread() { System.out.println("Bread()"); }
2015-06-15 17:47:35 -07:00
}
class Cheese {
2015-11-03 12:00:44 -08:00
Cheese() { System.out.println("Cheese()"); }
2015-06-15 17:47:35 -07:00
}
class Lettuce {
2015-11-03 12:00:44 -08:00
Lettuce() { System.out.println("Lettuce()"); }
2015-06-15 17:47:35 -07:00
}
class Lunch extends Meal {
2015-11-03 12:00:44 -08:00
Lunch() { System.out.println("Lunch()"); }
2015-06-15 17:47:35 -07:00
}
class PortableLunch extends Lunch {
2016-01-25 18:05:55 -08:00
PortableLunch() {
System.out.println("PortableLunch()");
}
2015-06-15 17:47:35 -07:00
}
public class Sandwich extends PortableLunch {
private Bread b = new Bread();
private Cheese c = new Cheese();
private Lettuce l = new Lettuce();
2016-01-25 18:05:55 -08:00
public Sandwich() {
System.out.println("Sandwich()");
}
2015-06-15 17:47:35 -07:00
public static void main(String[] args) {
new Sandwich();
}
2015-09-07 11:44:36 -06:00
}
/* Output:
2015-06-15 17:47:35 -07:00
Meal()
Lunch()
PortableLunch()
Bread()
Cheese()
Lettuce()
Sandwich()
2015-09-07 11:44:36 -06:00
*/