0

I created 4 classes: Cd, Dvd, Item and Database. Cd and Dvd extend from Item. The Database class stores an ArrayList of Items.

I'm stuck on creating a method in the Database class, which calls the method display in either Cd or Dvd. However I managed to display the Cds.

How can display all items? E.g.

Item number = 1
CD

Item number = 2
DVD

Item number = 3
Cd

Many thanks.

Edit:

The display methods are different in Cd and Dvd.

public void displayAll() {

    for (int i = 0; i < items.size(); i++) {

        Cd theCd = (Cd) items.get(i);
        // Dvd theDVD = (Dvd) items.get(i);

        System.out.println("Item Number = " + i);
        theCd.display();
         
        // theDvd.display
             
    }

}

My class diagram is:

Class Diagram http://img571.imageshack.us/img571/1460/unledsca.png

5 Answers 5

1

You probably should have public abstract void display(); in your Item class. One would imagine it makes sense for Itemss to be able to display themselves. Then you would implement it in CD and DVD (sounds like you already have). Then you'd have no need for the downcast:

public void displayAll()
{        
     for (int i = 0; i < items.size(); i++) {  
         Item item = (Item) items.get(i); 
         System.out.println("Item Number = " + i);
         item.display();
     }
}

And if you're using a JDK version that supports generics, consider using ArrayList<Item> instead of ArrayList. Then you can get rid of the Item cast as well.

Sign up to request clarification or add additional context in comments.

Comments

0

You should get Item from the list, and not a Cd, then just invoke the methods declared in the Item class. The methods in the subclasses will automatically be invoked if they are overriding methods.

Edit: Just checked your classdiagram. The method display should be added to the Item class, and overridden by the subclasses.

1 Comment

the display methods are different in cd and dvd
0

You're looking for instanceof

if (item instanceof Cd)

However, as others have said ... it may point toward a bad class design. Why do you need to know what the type is?

1 Comment

Yeah ... I went back and forth on that, and how now edited. There are cases where instanceof can make sense, but I don't know that this is one.
0

Don't explicitly cast; that defeats the purpose of polymorphism. Declare an abstract display method in your Item base class, and then override it in Cd and Dvd.

Then your loop can be:

List<Item> my_list = new ArrayList<Item>();

...

for (int i = 0; i < items.size(); i++) {
    System.out.println("Item number = " + i);
    items[i].display();
}

This will always call the correct method.

Comments

0
 for (int i = 0; i < items.size(); i++) {  


     Item theItem = (Item)items.get(i); 

     System.out.println("Item Number = " + i);
     theItem.display();


 }

Cast to Item, instead of CD or DVD. Make sure Item has a display() method. Also, if you are using Java 5 or above, have a look at generics

Comments

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.