Home : Course Map : Chapter 4 : Java :
Demos 3: Constructors
JavaTech
Course Map
Chapter 4

Introduction
Inheritance
  Demo 1
Overriding
  Demo 2a
  Demo 2b
this,super
MoreConstructors
  Demo 3
Abstraction
Interface 
  Demo 4
Casting References
MoreAboutArrays
Object class
Class Summary
Exercises

    Supplements
Override/Overload
Annotation-J2SE5.0
Java Security
Class Loading
Class Verifier
SecurityManager
     About JavaTech
     Codes List
     Exercises
     Feedback
     References
     Resources
     Tips
     Topic Index
     Course Guide
     What's New

In this demo we use the StartApplet3.java starter, which includes a class called Test and its subclass Test1, which in turn is subclassed by Test2.

Each class includes two constructors. We use this() and super() to create different paths for the sequence of constructors invoked in the building of the Test2 objects.

Two Test2 objects are created, each from a different starting constructor. Each constructor prints out a message to indicate it was invoked. Write down the sequence of messages before looking at the printout to see if you understand how the sequence of constructors is built up.

ConstructApplet.java
(Output goes to browser's Java console.)

public class ConstructApplet extends java.applet.Applet
{
  public void init () {
    System.out.println ("First test2 object");
    Test2 test2 = new Test2 (1.2, 1.3);
    System.out.println ();

    System.out.println ("Second test2 object");
    test2 = new Test2 (true, 1.2, 1.3);
  }

  // Paint message in Applet window.
  public void paint (java.awt.Graphics g) {
     g.drawString ("ConstructApplet", 10, 20);
  }
}

/** Test is our base class. **/
class Test
{
  int i;
  double d;
  boolean flag;

  Test ()  {
    d = 1.1;
    flag = true;
    System.out.println ("In Test  ()");
  }

  Test (int j) {
    this ();
    i = j;
    System.out.println ("In Test  (int j)");
  }
}

/** Test 1 is a subclass of Test. **/
class Test1 extends Test
{
  int k;

  Test1 (boolean b) {
    super (3);
    flag = b;
    System.out.println ("In Test1 (boolean b)");
  }

  Test1 (boolean b, int j) {
    this (b);
    k = j;
    System.out.println ("In Test1 (boolean b, int j)");
  }
}

/** And Test2 is a subclass of Test1. **/
class Test2 extends Test1
{
  double x,y;

  Test2 (double x, double y) {
    super (false);
    this.x = x;
    this.y = y;
    System.out.println ("In Test2 (double x, double y)");
  }

  Test2 (boolean b, double x, double y) {
    super (b,5);
    flag = b;
    System.out.println ("In Test2 (boolean b, double x, double y)");
  }
}

 

Latest Update: Oct. 21, 2004

            Tech
MoreComplexClass
ImprovedHistogram
JavaRandomNums
Vectors & Matrices
Exercises

           Physics
Runge-Kutta 2nd
  Demo 1
Runge-Kutta 4th
  Demo 2
BoundaryVal.Prob
Shooting Method
  Demo 3
Exercises

  Part I Part II Part III
Java Core 1  2  3  4  5  6  7  8  9  10  11  12 13 14 15 16 17
18 19 20
21
22 23 24
Supplements

1  2  3  4  5  6  7  8  9  10  11  12

Tech 1  2  3  4  5  6  7  8  9  10  11  12
Physics 1  2  3  4  5  6  7  8  9  10  11  12

Java is a trademark of Sun Microsystems, Inc.