|
(And we have discussed them in the past few webpages)
|
Fact:
|
(We will discussed constructor methods in this webpage)
|
|
Common sense/human nature:
|
|
|
|
public class GeometricObject { public String color; public boolean isFilled; public GeometricObject( ) // Constructor 1 { System.out.println("Calling: GeometricObject( )"); // Print announcement color = "BLANK"; isFilled = false; } public GeometricObject( String c, boolean f) // Constructor 2 { System.out.println("Calling: GeometricObject("+c+","+f+")"); color = c; isFilled = f; } ... (Other methods omitted for brevity) } |
(Now we can tell when and which constructor method has been called !!!)
public class Circle extends GeometricObject { .... public Circle( String c, boolean f, double r ) { super(c,f); // Invokes a constructor method in super class // Will invoke GeometricObject(String, boolean) // NOTE: Must be FIRST statement !!! radius = r; } ... } |
Comment:
|
public class testProg { public static void main(String[] args) { Circle x = new Circle ("red", false, 3); // Create a Circle object ... } } |
The new Circle ("red", false, 3) statement will print out:
Calling: GeometricObject(red,false) |
Explanation:
new Circle ("red", false, 3) will invoke the constructor: | | +-----> public Circle( String c, boolean f, double r ) { super(c,f) will invoke the constructor: | | +---> public GeometricObject( String c, boolean f) { print: Calling: GeometricObject(red,false) } ^ | | That's why you see "Calling: GeometricObject(red,false)" |
How to run the program:
|
public class GeometricObject { public String color; public boolean isFilled; public GeometricObject( ) // Constructor 1 { System.out.println("Calling: GeometricObject( )"); // Print announcement color = "BLANK"; isFilled = false; } public GeometricObject( String c, boolean f) // Constructor 2 { System.out.println("Calling: GeometricObject("+c+","+f+")"); color = c; isFilled = f; } ... (Other methods omitted for brevity) } |
(Now we can tell when and which constructor method has been called !!!)
public class Circle extends GeometricObject { .... public Circle( double r ) { radius = r; // Note: no super() call !!! } ... } |
Comment:
|
public class testProg { public static void main(String[] args) { Circle x = new Circle (3); // Create a Circle object ... } } |
The new Circle (3) statement will print out:
Calling: GeometricObject( ) |
Explanation:
new Circle ( 3 ) will invoke the constructor: | | +-----> public Circle( String c, boolean f, double r ) { // Inserted by Java compiler ! super( ) will invoke the constructor: | | +---> public GeometricObject( ) { print: Calling: GeometricObject( ) } ^ | | That's why you see "Calling: GeometricObject( )" |
How to run the program:
|
|
|
Example:
public class Circle extends GeometricObject { .... public Circle( String c, boolean f, double r ) { super( c ); // There is no constructor: // GeometricObject( String color ) // defined inside the base class !!! radius = r; } ... } |
Result: compiler error
>> javac Circle.java Circle.java:7: cannot find symbol symbol : constructor GeometricObject(java.lang.String) location: class GeometricObject super( c ); |
How to run the program:
|
|
Example:
public class GeometricObject { public String color; public boolean isFilled; // NO: public GeometricObject( ) !!!! public GeometricObject( String c, boolean f) { System.out.println("Calling: GeometricObject("+c+","+f+")"); color = c; isFilled = f; } .... } |
Result: compiler error
Java 1.6 compiler: >> javac Circle.java Circle.java:13: cannot find symbol symbol : constructor GeometricObject() location: class GeometricObject |
How to run the program:
|