Constructors and polymorphism don't produce what you might expect : Constructor « Class « Java






Constructors and polymorphism don't produce what you might expect

Constructors and polymorphism don't produce what you might expect
 

// : c07:PolyConstructors.java
// Constructors and polymorphism don't produce what you might expect.
// From 'Thinking in Java, 3rd ed.' (c) Bruce Eckel 2002
// www.BruceEckel.com. See copyright notice in CopyRight.txt.

abstract class Glyph {
  abstract void draw();

  Glyph() {
    System.out.println("Glyph() before draw()");
    draw();
    System.out.println("Glyph() after draw()");
  }
}

class RoundGlyph extends Glyph {
  private int radius = 1;

  RoundGlyph(int r) {
    radius = r;
    System.out.println("RoundGlyph.RoundGlyph(), radius = " + radius);
  }

  void draw() {
    System.out.println("RoundGlyph.draw(), radius = " + radius);
  }
}

public class PolyConstructors {
  public static void main(String[] args) {
    new RoundGlyph(5);

  }
} ///:~


           
         
  








Related examples in the same category

1.Paying attention to exceptions in constructorsPaying attention to exceptions in constructors
2.Order of constructor callsOrder of constructor calls
3.Constructor initialization with compositionConstructor initialization with composition
4.Demonstration of a simple constructorDemonstration of a simple constructor
5.Constructors can have argumentsConstructors can have arguments
6.Show Constructors conflicting
7.Show that if your class has no constructors, your superclass constructors still get calledShow that if your class has no constructors, your superclass constructors still get called
8.Constructor calls during inheritanceConstructor calls during inheritance
9.A constructor for copying an object of the sameA constructor for copying an object of the same
10.Create a new instance of a class by calling a constructor with arguments