Reputation: 793
I have a superclass (SimpleGeometricObject) which is extended to two subclasses (CircleFromSimpleGeometricObject and RectangleFromSimpleGeometricObject), and a class that invokes CircleFromSimpleGeometricObject and RectangleFromSimpleGeometricObject called TestCircleRectangle. Following the debugger, for subclass CircleFromSumpleGeometricObject, this line of code:
public CircleFromSimpleGeometricObject(double radius){
this.radius = radius;
}
somehow invokes the superclass SimpleGeometricObject:
/** Construct a default geometric object */
public SimpleGeometricObject() {
dateCreated = new java.util.Date();
}
I am a bit confused about how this happens and why, can someone help me understand why this happens? Below are the codes to all the classes.
public class SimpleGeometricObject {
private String color = "white";
private boolean filled;
private java.util.Date dateCreated;
/** Construct a default geometric object */
public SimpleGeometricObject() {
dateCreated = new java.util.Date();
}
/** Construct a geometric object with the specified color
* and filled value */
public SimpleGeometricObject(String color, boolean filled) {
dateCreated = new java.util.Date();
this.color = color;
this.filled = filled;
}
/** Return color */
public String getColor() {
return color;
}
/** Set a new color */
public void setColor(String color) {
this.color = color;
}
/** Return filled. Since filled is boolean,
its get method is named isFilled */
public boolean isFilled() {
return filled;
}
/** Set a new filled */
public void setFilled(boolean filled) {
this.filled = filled;
}
/** Get dateCreated */
public java.util.Date getDateCreated() {
return dateCreated;
}
/** Return a string representation of this object */
public String toString() {
return "created on " + dateCreated + "\ncolor: " + color +
" and filled: " + filled;
}
}
public class CircleFromSimpleGeometricObject
extends SimpleGeometricObject {
private double radius;
public CircleFromSimpleGeometricObject() {
}
public CircleFromSimpleGeometricObject(double radius){
this.radius = radius;
}
public CircleFromSimpleGeometricObject(double radius,
String color, boolean filled) {
this.radius = radius;
setColor(color);
setFilled(filled);
}
/** Return radius */
public double getRadius() {
return radius;
}
/** Set a new radius */
public void setRadius(double radius) {
this.radius = radius;
}
/** Return area */
public double getArea() {
return radius * radius * Math.PI;
}
/** Return diameter */
public double getDiameter() {
return 2 * radius;
}
/** Return perimeter */
public double getPerimeter() {
return 2 * radius * Math.PI;
}
/** Print the circle info */
public void printCircle() {
System.out.println("The circle is created " + getDateCreated() +
" and the radius is " + radius);
}
}
public class RectangleFromSimpleGeometricObject
extends SimpleGeometricObject {
private double width;
private double height;
public RectangleFromSimpleGeometricObject() {
}
public RectangleFromSimpleGeometricObject(
double width, double height) {
this.width = width;
this.height = height;
}
public RectangleFromSimpleGeometricObject(
double width, double height, String color, boolean filled) {
this.width = width;
this.height = height;
setColor(color);
setFilled(filled);
}
/** Return width */
public double getWidth() {
return width;
}
/** Set a new width */
public void setWidth(double width) {
this.width = width;
}
/** Return height */
public double getHeight() {
return height;
}
/** Set a new height */
public void setHeight(double height) {
this.height = height;
}
/** Return area */
public double getArea() {
return width * height;
}
/** Return perimeter */
public double getPerimeter() {
return 2 * (width * height);
}
}
public class TestCircleRectangle {
public static void main(String[] args) {
CircleFromSimpleGeometricObject circle =
new CircleFromSimpleGeometricObject(1);
System.out.println("A circle " + circle.toString());
System.out.println("The color is " + circle.getColor());
System.out.println("The radius is " + circle.getRadius());
System.out.println("The area is " + circle.getArea());
System.out.println("The diamter is " + circle.getDiameter());
RectangleFromSimpleGeometricObject rectangle =
new RectangleFromSimpleGeometricObject(2, 4);
System.out.println("\nA rectangle " + rectangle.toString());
System.out.println("The area is " + rectangle.getArea());
System.out.println("The perimeter is " +
rectangle.getPerimeter());
}
}
Upvotes: 2
Views: 1510
Reputation: 11
Since CircleFormSimpleGeometricObject extends SimpleGeometricObject, the constructor SimpleGeometricObject() is automatically called when the constructor CircleFormSimpleGeometricObject() is invoked.
This is useful since any variables that a subclass needs from the superclass would be initialized, though if this is the case it is safer to call super(), which explicitly calls the superclass's constructor. To specify which constructor to use, the variables that the specific constructor calls for can be put into super(). For example, this line:
super(String, boolean);
would call the related constructor in SimpleGeometricObject.
Upvotes: 0
Reputation: 3885
According to the java specification, all object constructors implicitly call their super class's constructors. Imagine if it didn't: your dateCreated
object would not be initialized.
Here is the blog I found that points to the java specification:
http://www.dribin.org/dave/blog/archives/2004/11/23/java_constructor/
Upvotes: 0
Reputation: 38195
Any constructor invokes the super-class constructor (and so on, until the Object
constructor is invoked). If you do not explicitly call super()
, the compiler inserts it for you.
The most obvious way to see this is to have no default constructor in your superclass (no constructor without any arguments). In this case, you subclass will not compile until you insert an explicit call to the super
constructor you want.
Upvotes: 0
Reputation: 81674
A constructor like public CircleFromSimpleGeometricObject(double radius)
always must include a call to its superclass's constructor as the first line; if you don't do it explicitly, the compiler will invisibly insert a call to the superclass's no-argument constructor, if it has one. That's what has happened here; the constructor is automatically calling public SimpleGeometricObject()
.
A constructor can call a superclass constructor like this:
super();
You could include arguments, if any are required.
P.S. As a commenter mentioned, your class names are really odd and unnecessary; Circle
and Rectangle
would be sufficient.
Upvotes: 4