OnJava8-Examples/polymorphism/PolyConstructors.java

42 lines
982 B
Java
Raw Normal View History

2015-09-07 11:44:36 -06:00
// polymorphism/PolyConstructors.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.
2015-06-15 17:47:35 -07:00
// Constructors and polymorphism
2016-01-25 18:05:55 -08:00
// don't produce what you might expect
2015-06-15 17:47:35 -07:00
class Glyph {
2015-11-03 12:00:44 -08:00
void draw() { System.out.println("Glyph.draw()"); }
2015-06-15 17:47:35 -07:00
Glyph() {
2015-11-03 12:00:44 -08:00
System.out.println("Glyph() before draw()");
2015-06-15 17:47:35 -07:00
draw();
2015-11-03 12:00:44 -08:00
System.out.println("Glyph() after draw()");
2015-06-15 17:47:35 -07:00
}
}
class RoundGlyph extends Glyph {
private int radius = 1;
RoundGlyph(int r) {
radius = r;
2015-12-02 09:20:27 -08:00
System.out.println(
"RoundGlyph.RoundGlyph(), radius = " + radius);
2015-06-15 17:47:35 -07:00
}
@Override
void draw() {
2015-12-02 09:20:27 -08:00
System.out.println(
"RoundGlyph.draw(), radius = " + radius);
2015-06-15 17:47:35 -07:00
}
}
public class PolyConstructors {
public static void main(String[] args) {
new RoundGlyph(5);
}
2015-09-07 11:44:36 -06:00
}
/* Output:
2015-06-15 17:47:35 -07:00
Glyph() before draw()
RoundGlyph.draw(), radius = 0
Glyph() after draw()
RoundGlyph.RoundGlyph(), radius = 5
2015-09-07 11:44:36 -06:00
*/