0

I have the main method written as following:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package excercise.pkg5;

/**
 *
 * @author Azraar
 */

    public class TestResizable {

    public static void main(String[] args) {

        Shape obj[] = new Shape[4];
        obj[0] = new Circle(10);
        obj[1] = new Rectangle(10, 20);
        obj[2] = new ResizableCircle(10);
        obj[3] = new ResizableRectangle(10, 20);

        for (int i = 0; i < obj.length; i++) {

            if (obj[i] instanceof ResizableCircle) {
                ResizableCircle r = (ResizableCircle) obj[i];
                obj[i].equals(r);

            }

            if (obj[i] instanceof ResizableRectangle) {
                ResizableRectangle r = (ResizableRectangle) obj[i];
                obj[i].equals(r);

            }

            System.out.println("");
            System.out.print("Object is - " + obj[i].name());
            System.out.print("\nObject Area is - " + obj[i].area());
            System.out.print("\nObject Perimeter is - " + obj[i].perimeter());
        }

    }
}

i am using ResizableRectangle r = (ResizableRectangle) obj[i]; , because resizable is an implement and ResizableRectangle and ResizableCircle are extending it and overriding the method resize.

if instanceof resizbleRectange or resizableCircle.. i need to run this resize() method..

obj[i].resize(0.5) and then it will loop out and print. but the problem is i am not getting resize method true intelisense also when i type it says cannot find sympbol...

below is the class hierarchy...

enter image description here

EDIT:

shape is the ROOT CLASS as you can see in the attached screenshot. i am thinking of a way to access resize method when object is an instace of ResizeableClass and ResizeableRectangle. but still unable.

4 Answers 4

2

You need to call resize() on a Resizeable reference.

That is, if obj[i] is a Resizeable, then you need to cast and then call it. It's not enough simply to assert that obj[i] refers to such an object since the compiler is simply treating it as a base class reference.

e.g.

if (obj[i] instanceof Resizeable) {
   ((Resizeable)obj[i]).resize(..);
}
11
  • is it possible for you to show me an example with what i have done ?
    – dev1234
    Commented Sep 13, 2013 at 12:20
  • 1
    See above for a trivial example Commented Sep 13, 2013 at 12:20
  • 1
    You cannot create object of Resizable. you can only use it as polymorphic reference. Commented Sep 13, 2013 at 12:26
  • 1
    A ResizableRectangle IS A Resizable. Did they teach you IS A, and HAS A relationships?
    – Cruncher
    Commented Sep 13, 2013 at 12:28
  • 1
    @mazraara implements maintains is a. You can have an array of elements of an interface, even though none of them would be an object of that interface. However since anything subclassing it IS A of the interface, you can use all of the methods defined in the interface on the object.
    – Cruncher
    Commented Sep 13, 2013 at 12:32
1

You declared obj as

Shape obj[]

so obj[i] will return a Shape object which I'm guessing doesn't extend Resizeable.

3
  • yes shape is the ROOT CLASS as you can see in the attached screenshot. i am thinking of a way to access resize method when object is an instace of ResizeableClass and ResizeableRectangle.
    – dev1234
    Commented Sep 13, 2013 at 12:26
  • 1
    Actually, can't see the name Shape in the screenshot :) Anyway, that's the point, there's no guarantee that an instance of Shape will implement Resizable; i.e. Circle extends Shape but does not implement Resizable. As @BrianAgnew answered, you may check that obj[i] is indeed an instance of Resizable, and then cast.
    – ssantos
    Commented Sep 13, 2013 at 12:31
  • Sorry my bad, i captured it that way. pls check the edited question above. Shape is the root class as in hidden screenshot.
    – dev1234
    Commented Sep 13, 2013 at 12:33
1

Not all subclasses of Shape method implement Resizable interface. So you need to check if obj[i] is instance of Resizeable class and then call resize on it.

0

This worked at last...

    if (obj[i] instanceof ResizableCircle) {
        Resizable c = (ResizableCircle) obj[i];
        c.resize(5);

    }

    if (obj[i] instanceof ResizableRectangle) {
        Resizable r = (ResizableRectangle) obj[i];
        r.resize(10);

    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.