2015-09-07 11:44:36 -06:00
|
|
|
// polymorphism/Frog.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
|
|
|
// Cleanup and inheritance
|
2016-07-07 15:12:55 -06:00
|
|
|
// {main: polymorphism.Frog}
|
2015-06-15 17:47:35 -07:00
|
|
|
package polymorphism;
|
|
|
|
|
|
|
|
class Characteristic {
|
|
|
|
private String s;
|
|
|
|
Characteristic(String s) {
|
|
|
|
this.s = s;
|
2015-11-03 12:00:44 -08:00
|
|
|
System.out.println("Creating Characteristic " + s);
|
2015-06-15 17:47:35 -07:00
|
|
|
}
|
|
|
|
protected void dispose() {
|
2015-11-03 12:00:44 -08:00
|
|
|
System.out.println("disposing Characteristic " + s);
|
2015-06-15 17:47:35 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Description {
|
|
|
|
private String s;
|
|
|
|
Description(String s) {
|
|
|
|
this.s = s;
|
2015-11-03 12:00:44 -08:00
|
|
|
System.out.println("Creating Description " + s);
|
2015-06-15 17:47:35 -07:00
|
|
|
}
|
|
|
|
protected void dispose() {
|
2015-11-03 12:00:44 -08:00
|
|
|
System.out.println("disposing Description " + s);
|
2015-06-15 17:47:35 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class LivingCreature {
|
|
|
|
private Characteristic p =
|
|
|
|
new Characteristic("is alive");
|
|
|
|
private Description t =
|
|
|
|
new Description("Basic Living Creature");
|
|
|
|
LivingCreature() {
|
2015-11-03 12:00:44 -08:00
|
|
|
System.out.println("LivingCreature()");
|
2015-06-15 17:47:35 -07:00
|
|
|
}
|
|
|
|
protected void dispose() {
|
2015-11-03 12:00:44 -08:00
|
|
|
System.out.println("LivingCreature dispose");
|
2015-06-15 17:47:35 -07:00
|
|
|
t.dispose();
|
|
|
|
p.dispose();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Animal extends LivingCreature {
|
|
|
|
private Characteristic p =
|
|
|
|
new Characteristic("has heart");
|
|
|
|
private Description t =
|
|
|
|
new Description("Animal not Vegetable");
|
2015-11-03 12:00:44 -08:00
|
|
|
Animal() { System.out.println("Animal()"); }
|
2015-06-15 17:47:35 -07:00
|
|
|
@Override
|
|
|
|
protected void dispose() {
|
2015-11-03 12:00:44 -08:00
|
|
|
System.out.println("Animal dispose");
|
2015-06-15 17:47:35 -07:00
|
|
|
t.dispose();
|
|
|
|
p.dispose();
|
|
|
|
super.dispose();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Amphibian extends Animal {
|
|
|
|
private Characteristic p =
|
|
|
|
new Characteristic("can live in water");
|
|
|
|
private Description t =
|
|
|
|
new Description("Both water and land");
|
|
|
|
Amphibian() {
|
2015-11-03 12:00:44 -08:00
|
|
|
System.out.println("Amphibian()");
|
2015-06-15 17:47:35 -07:00
|
|
|
}
|
|
|
|
@Override
|
|
|
|
protected void dispose() {
|
2015-11-03 12:00:44 -08:00
|
|
|
System.out.println("Amphibian dispose");
|
2015-06-15 17:47:35 -07:00
|
|
|
t.dispose();
|
|
|
|
p.dispose();
|
|
|
|
super.dispose();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public class Frog extends Amphibian {
|
|
|
|
private Characteristic p = new Characteristic("Croaks");
|
|
|
|
private Description t = new Description("Eats Bugs");
|
2015-11-03 12:00:44 -08:00
|
|
|
public Frog() { System.out.println("Frog()"); }
|
2015-06-15 17:47:35 -07:00
|
|
|
@Override
|
|
|
|
protected void dispose() {
|
2015-11-03 12:00:44 -08:00
|
|
|
System.out.println("Frog dispose");
|
2015-06-15 17:47:35 -07:00
|
|
|
t.dispose();
|
|
|
|
p.dispose();
|
|
|
|
super.dispose();
|
|
|
|
}
|
|
|
|
public static void main(String[] args) {
|
|
|
|
Frog frog = new Frog();
|
2015-11-03 12:00:44 -08:00
|
|
|
System.out.println("Bye!");
|
2015-06-15 17:47:35 -07:00
|
|
|
frog.dispose();
|
|
|
|
}
|
2015-09-07 11:44:36 -06:00
|
|
|
}
|
|
|
|
/* Output:
|
2015-06-15 17:47:35 -07:00
|
|
|
Creating Characteristic is alive
|
|
|
|
Creating Description Basic Living Creature
|
|
|
|
LivingCreature()
|
|
|
|
Creating Characteristic has heart
|
|
|
|
Creating Description Animal not Vegetable
|
|
|
|
Animal()
|
|
|
|
Creating Characteristic can live in water
|
|
|
|
Creating Description Both water and land
|
|
|
|
Amphibian()
|
|
|
|
Creating Characteristic Croaks
|
|
|
|
Creating Description Eats Bugs
|
|
|
|
Frog()
|
|
|
|
Bye!
|
|
|
|
Frog dispose
|
|
|
|
disposing Description Eats Bugs
|
|
|
|
disposing Characteristic Croaks
|
|
|
|
Amphibian dispose
|
|
|
|
disposing Description Both water and land
|
|
|
|
disposing Characteristic can live in water
|
|
|
|
Animal dispose
|
|
|
|
disposing Description Animal not Vegetable
|
|
|
|
disposing Characteristic has heart
|
|
|
|
LivingCreature dispose
|
|
|
|
disposing Description Basic Living Creature
|
|
|
|
disposing Characteristic is alive
|
2015-09-07 11:44:36 -06:00
|
|
|
*/
|