Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Problem with subclass and superclass methods

  1. #1
    Junior Member
    Join Date
    Jul 2011
    Location
    Omaha, NE
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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]


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,537
    My Mood
    Grumpy
    Thanks
    0
    Thanked 166 Times in 157 Posts

    Default Re: Problem with subclass and superclass methods

    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.
    Improving the world one idiot at a time!

  3. #3
    Junior Member
    Join Date
    Jul 2011
    Location
    Omaha, NE
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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]

  4. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,537
    My Mood
    Grumpy
    Thanks
    0
    Thanked 166 Times in 157 Posts

    Default Re: Problem with subclass and superclass methods

    Quote Originally Posted by Farmer View Post
    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.
    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.
    Improving the world one idiot at a time!

  5. #5
    Junior Member
    Join Date
    Jul 2011
    Location
    Omaha, NE
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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

  6. #6
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,537
    My Mood
    Grumpy
    Thanks
    0
    Thanked 166 Times in 157 Posts

    Default Re: Problem with subclass and superclass methods

    Quote Originally Posted by Farmer View Post
    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.
    Improving the world one idiot at a time!

Similar Threads

  1. [SOLVED] Help regarding Superclass variable can reference subclass object
    By rohan22 in forum Java Theory & Questions
    Replies: 8
    Last Post: July 12th, 2011, 01:30 AM
  2. [SOLVED] Invoking a superclass constructor in my subclass
    By kari4848 in forum Object Oriented Programming
    Replies: 5
    Last Post: April 29th, 2011, 01:12 PM
  3. Synchronized Methods problem!
    By RiskyShenanigan in forum AWT / Java Swing
    Replies: 1
    Last Post: November 28th, 2010, 11:04 AM
  4. Instantiating a SuperClass Map from a SubClass
    By drexasaurus in forum Collections and Generics
    Replies: 1
    Last Post: September 9th, 2010, 10:51 PM
  5. Completely Lost in this problem(multiple methods)
    By wacarter in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 31st, 2010, 04:44 PM