Problem with subclass and superclass methods
Hello,
I am having trouble with the code posted below. The code has some pre-compile errors, which I will list here:
//cannot find symbol
//symbol: method showStyle(hsdynoveriddenshapes1.TwoDShape)
//location: class hsdynoveriddenshapes1.HSDynOveriddenShapes1
The error has to do with the showStyle() method called in lines 106 and 107. If I write similar print statements but for the showDim() method I get similar errors.
I want the code to at least output this:
object is triangle
style is right
Area is 48.0
object is triangle
style is isosceles
Area is 24.5
object is null
style is null
Area is 0.0
If I could use the getDim() method to work that would be good too. Here is the code.
[highlight=java]
package hsdynoveriddenshapes1;
class TwoDShape {
private double width;
private double height;
private String name;
// A default constructor.
TwoDShape() {
width = height = 0.0;
name = "null";
//System.out.println("width is: " + width); // This prints with correct object.
}
// Parameterized constructor.
TwoDShape(double w, double h, String n) {
width = w;
height = h;
name = n;
//System.out.println("name is: " + name); This prints with correct object.
}
// Construct object with equal width and height.
TwoDShape(double x, String n) {
width = height = x;
name = n;
}
// Construct an object from an object.
TwoDShape(TwoDShape ob) {
width = ob.width;
height = ob.height;
name = ob.name;
System.out.println("name is: " + name); //Why won't this print?
}
// Accessor methods for width and height.
double getWidth() { return width; }
double getHeight() { return height; }
void setWidth(double w) { width = w; }
void setHeight(double h) { height = h; }
String getName() { return name; }
void showDim() {
System.out.println("Width and height are " +
width + " and " + height);
}
double area() {
System.out.println("area() must be overridden");
return 0.0;
}
}
// A subclass of TwoDShape for Triangles.
class Triangle extends TwoDShape {
private String style;
// A default constructor.
Triangle() {
super();
style = "null";
}
// Constructor for Triangle.
Triangle(String s, double w, double h) {
super(w, h, "triangle");
style = s;
}
// Construct an isosceles triangle.
Triangle(double x) {
super(x, "triangle"); // call superclass constructor
style = "isosceles";
}
// Construct an object from an object.
Triangle(Triangle ob) {
super(ob); // pass object to TwoDShape constructor
style = ob.style;
System.out.println("style is " + style);
}
@Override
double area() {
return getWidth() * getHeight() / 2;
}
void showStyle() {
System.out.println("Triangle is " + style);
}
}
public class HSDynOveriddenShapes1 {
public static void main(String[] args) {
TwoDShape shapes[] = new TwoDShape[3];
shapes[0] = new Triangle("right", 8.0, 12.0);
shapes[1] = new Triangle(7.0);
shapes[2] = new Triangle();
//shapes[3] = new TwoDShape(10,20,"Generic");
for(int i=0; i < shapes.length; i++) {
System.out.println("object is " + shapes[i].getName());
System.out.println("Area is " + shapes[i].area());
System.out.println("Style is " + showStyle(shapes[i]));
System.out.println("Style is " + shapes[i].showStyle());
System.out.println();
}
}
}
[/java]
Re: Problem with subclass and superclass methods
Code java:
System.out.println("Style is " + showStyle(shapes[i]));
On that line you are calling the showStyle method and passing a single parameter. The only showStyle method I can find in your code has no parameters.
Re: Problem with subclass and superclass methods
Junky,
Thanks for your response, I have been working through this book from the public library on Java. Right now I am working with overriding methods and inheritance. The code I included before is a portion of a larger piece of code (two subclasses and a superclass). Instead of writing the forum everytime I have a problem, I work on through more examples and some problems clear up other don't.
I see your point showStyle() doesn't take parameters, that slipped by me. At the same time however, the method calls: shapes[i].getName() and shapes[i].area() from from the following block of code execute just fine and their methods do not declare parameters. Why are they working? By the way, the comments in the block below may help explain things better and hopefully the code tags work.
[code=java]
[highlight=java]
public static void main(String[] args) {
TwoDShape shapes[] = new TwoDShape[3];
shapes[0] = new Triangle("right", 8.0, 12.0);
shapes[1] = new Triangle(7.0);
shapes[2] = new Triangle();
//shapes[3] = new TwoDShape(10,20,"Generic");
for(int i=0; i < shapes.length; i++) {
System.out.println("object is " + shapes[i].getName()); //Output is: "object is triangle"
System.out.println("Area is " + shapes[i].area()); // Output is: "area is 48"
System.out.println("Style is " + showStyle(shapes[i])); // I was just trying anything here, oops method doesn't take parameters
System.out.println("Style is " + shapes[i].showStyle()); //Output should be: "triangle is right"
System.out.println();
}
[/java]
[/java]
Re: Problem with subclass and superclass methods
Quote:
Originally Posted by
Farmer
I see your point showStyle() doesn't take parameters, that slipped by me. At the same time however, the method calls: shapes[i].getName() and shapes[i].area() from from the following block of code execute just fine and their methods do not declare parameters. Why are they working?
It has nothing to do with if you have parameters or not. What is does have to wdo with is if the parameters match.
Code java:
class Foo {
public void methodOne() {
System.out.println("Lime");
}
public void methodOne(String s) {
System.out.println("Apple");
}
public void methodOne(int i) {
System.out.println("Pear");
}
public void methodTwo(String s, int i) {
System.out.println("Cherry");
}
public void methodTwo(int i, String s) {
System.out.println("Banana");
}
public static void main(String[] args) {
Foo f = new Foo();
f.methodOne("hello");
f.methodTwo("hello", 42);
}
}
Run that code and make sure you understand why you get the output you do.
Re: Problem with subclass and superclass methods
Junky,
When I compile your code, I get the following output:
Apple
Cherry
I get apple and cherry for these reasons starting at main():
Foo f = new Foo();
A new object of type Foo is created with the name f.
f.methodOne("hello");
methodOne(String s) is called passing the string "hello" to methodOne(String s).
The statement in the method block says; "System.out.println("Apple");"
This sends Apple to the output.
f.methodTwo("hello", 42);
methodTwo(String s, int i) is called passing the string "hello" to methodTwo(String s, int i).
The statement in the method block says; "System.out.println("Cherry");"
This sends Apple to the output.
What if I wanted to print "Bannana"?
f.methodTwo(10, ''yellow");
methodTwo(int i, String s) is called passing the string "hello" to methodTwo(int i, String s).
The statement in the method block says; "System.out.println("Bannana");"
This sends Bannana to the output.
The method in my code looks more like methodOne(), with no parameters. In this code, you would call it in main
like this: f.methodOne(); and it would print lime.
I suppose you getting to this but in my code, why can't I use the call shapes[i].style()? It uses the format: object.method(), like f.methodOne(). I call shapes[i].area() and shapes[i].getName(), these methods work fine.
Thanks
Re: Problem with subclass and superclass methods
Quote:
Originally Posted by
Farmer
I call shapes[i].area() and shapes[i].getName(), these methods work fine.
Of course they work because a method called area that has no parameters exists. A method called getName that has no parameters exists. What doesn't exist is a method called showStyle that has one parameter of type TwoDShape.