OnJava8-Examples/polymorphism/PolyConstructors.java

38 lines
832 B
Java
Raw Normal View History

2015-06-15 17:47:35 -07:00
//: polymorphism/PolyConstructors.java
// <20>2015 MindView LLC: see Copyright.txt
// Constructors and polymorphism
// don't produce what you might expect.
import static com.mindviewinc.util.Print.*;
class Glyph {
void draw() { print("Glyph.draw()"); }
Glyph() {
print("Glyph() before draw()");
draw();
print("Glyph() after draw()");
}
}
class RoundGlyph extends Glyph {
private int radius = 1;
RoundGlyph(int r) {
radius = r;
print("RoundGlyph.RoundGlyph(), radius = " + radius);
}
@Override
void draw() {
print("RoundGlyph.draw(), radius = " + radius);
}
}
public class PolyConstructors {
public static void main(String[] args) {
new RoundGlyph(5);
}
} /* Output:
Glyph() before draw()
RoundGlyph.draw(), radius = 0
Glyph() after draw()
RoundGlyph.RoundGlyph(), radius = 5
*///:~