Results 1 to 5 of 5
  1. #1
    DylanDubya is offline Member
    Join Date
    Dec 2011
    Posts
    7
    Rep Power
    0

    Default Help me understand how this polymorphism example works?

    Java 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:
    Java Code:
    public Circle4(double radius, String color, boolean filled) {
        super(color, filled);
        this.radius = radius;
    Rectangle1:
    Java Code:
    public Rectangle1(double width, double height, String color, 
          boolean filled) {
        this.width = width;
        this.height = height;
        setColor(color);
        setFilled(filled);
    GeometricObject1:
    Java 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;
      }
    Last edited by DylanDubya; 09-21-2012 at 09:22 PM.

  2. #2
    thinkinjava24 is offline Member
    Join Date
    Sep 2012
    Posts
    17
    Rep Power
    0

    Default Re: Help me understand how this polymorphism example works?

    show your "getColor" method, please

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: Help me understand how this polymorphism example works?

    Also the entire Circle4 class.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    DylanDubya is offline Member
    Join Date
    Dec 2011
    Posts
    7
    Rep Power
    0

    Default Re: Help me understand how this polymorphism example works?

    Circle4 class:
    Java 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:
    Java 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:
    Java 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);
      }
    }

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default 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.
    Please do not ask for code as refusal often offends.

Similar Threads

  1. Polymorphism
    By kkelv in forum New To Java
    Replies: 0
    Last Post: 03-29-2012, 09:35 PM
  2. Compile-time Polymorphism or Run-time Polymorphism ?
    By dejulial in forum New To Java
    Replies: 1
    Last Post: 03-06-2012, 07:14 PM
  3. How to use Polymorphism
    By nfsmwbe in forum New To Java
    Replies: 10
    Last Post: 01-16-2012, 02:15 PM
  4. Program works but don't exactly understand it
    By Danieldcc in forum New To Java
    Replies: 4
    Last Post: 10-20-2010, 06:13 AM
  5. what's polymorphism?
    By christina in forum New To Java
    Replies: 2
    Last Post: 08-05-2007, 10:29 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •