# Help me understand how this polymorphism example works?

• 09-21-2012, 06:16 PM
DylanDubya
Help me understand how this polymorphism example works?
Code:

```public class PolymorphismDemo {   /** Main method */   public static void main(String[] args) {     // Display circle and rectangle properties     displayObject(new Circle4(1, "red", false));     displayObject(new Rectangle1(1, 1, "black", true));   }   /** Display geometric object properties */   public static void displayObject(GeometricObject1 object) {     System.out.println("Created on " + object.getDateCreated() +       ". Color is " + object.getColor());   } }```
Now when this code is ran, the output is:
Created on Fri Sep 21 11:07:11 CDT 2012. Color is white
Created on Fri Sep 21 11:07:11 CDT 2012. Color is black

I don't understand why the circle isn't red?

Oh, I guess I should probably post the constructors for the different objects too.
Circle4 and Rectangle1 both extend GeometricObject1. Here is their constructors

Circle4:
Code:

```public Circle4(double radius, String color, boolean filled) {     super(color, filled);     this.radius = radius;```
Rectangle1:
Code:

```public Rectangle1(double width, double height, String color,       boolean filled) {     this.width = width;     this.height = height;     setColor(color);     setFilled(filled);```
GeometricObject1:
Code:

```  public GeometricObject1() {     dateCreated = new java.util.Date();   }   public GeometricObject1(String Color, boolean filled) {     dateCreated = new java.util.Date();     this.color = color;     this.filled = filled;   }```
• 09-22-2012, 05:27 AM
thinkinjava24
Re: Help me understand how this polymorphism example works?
• 09-22-2012, 06:52 AM
DarrylBurke
Re: Help me understand how this polymorphism example works?
Also the entire Circle4 class.

db
• 09-24-2012, 08:30 AM
DylanDubya
Re: Help me understand how this polymorphism example works?
Circle4 class:
Code:

```package chapter11; public class Circle4 extends GeometricObject1 {   private double radius;   public Circle4() {   }   public Circle4(double radius) {         super();     this.radius = radius;   }   public Circle4(double radius, String color, boolean filled) {     super(color, 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(toString() + "The circle is created " + getDateCreated() +       " and the radius is " + radius);   }     public String toString() {     return "Circle WWWW " + getColor() + super.toString();   } }```
Geometric Object class:
Code:

```package chapter11; public class GeometricObject1 {   private String color = "white";   private boolean filled;   private java.util.Date dateCreated;     /** Construct a default geometric object */   public GeometricObject1() {     dateCreated = new java.util.Date();   }   /** Construct a geometric object with the specified color     *  and filled value */   public GeometricObject1(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;   } }```
Rectangle class:
Code:

```package chapter11; public class Rectangle1 extends GeometricObject1 {   private double width;   private double height;   public Rectangle1() {   }   public Rectangle1(double width, double height) {     this.width = width;     this.height = height;   }   public Rectangle1(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);   } }```
• 09-24-2012, 11:10 AM
Tolls
Re: Help me understand how this polymorphism example works?
Stick some debug in those classes, specifically around the color information.
I can't spot where it's going wrong, but it's possible you're running old code, which should become apparent if you debug it.