Results 1 to 5 of 5
- 09-21-2012, 06:16 PM #1
Member
- Join Date
- Dec 2011
- Posts
- 7
- Rep Power
- 0
Help me understand how this polymorphism example works?
Now when this code is ran, the output is: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()); } }
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:Rectangle1:Java Code:public Circle4(double radius, String color, boolean filled) { super(color, filled); this.radius = radius;GeometricObject1:Java Code:public Rectangle1(double width, double height, String color, boolean filled) { this.width = width; this.height = height; setColor(color); setFilled(filled);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.
- 09-22-2012, 05:27 AM #2
Member
- Join Date
- Sep 2012
- Posts
- 17
- Rep Power
- 0
Re: Help me understand how this polymorphism example works?
show your "getColor" method, please
- 09-22-2012, 06:52 AM #3
Re: Help me understand how this polymorphism example works?
Also the entire Circle4 class.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 09-24-2012, 08:30 AM #4
Member
- Join Date
- Dec 2011
- Posts
- 7
- Rep Power
- 0
Re: Help me understand how this polymorphism example works?
Circle4 class:
Geometric Object 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(); } }
Rectangle 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; } }
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); } }
- 09-24-2012, 11:10 AM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 10,448
- Rep Power
- 16
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
-
Polymorphism
By kkelv in forum New To JavaReplies: 0Last Post: 03-29-2012, 09:35 PM -
Compile-time Polymorphism or Run-time Polymorphism ?
By dejulial in forum New To JavaReplies: 1Last Post: 03-06-2012, 07:14 PM -
How to use Polymorphism
By nfsmwbe in forum New To JavaReplies: 10Last Post: 01-16-2012, 02:15 PM -
Program works but don't exactly understand it
By Danieldcc in forum New To JavaReplies: 4Last Post: 10-20-2010, 06:13 AM -
what's polymorphism?
By christina in forum New To JavaReplies: 2Last Post: 08-05-2007, 10:29 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks