Question: this is Java How come our method showPrice works with CompactDisc and DvdMovie objects? What can you say about the RetailItem class just by looking

this is Java

How come our method showPrice works with CompactDisc and DvdMovie objects? What can you say about the RetailItem class just by looking at this code fragment?

public class PolymorphicInterfaceDemo{ public static void main(String[] args) { CompactDisc cd = new CompactDisc("Greatest Hits", "Joe Looney Band", 18.95); DvdMovie movie = new DvdMovie("Wheels of Fury", 137, 12.95);

System.out.println("Item #1: " + cd.getTitle()); showPrice(cd); System.out.println("Item #2: " + movie.getTitle()); showPrice(movie); }

private static void showPrice(RetailItem item) { System.out.printf("Price: $%,.2f ", item.getRetailPrice());

} }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!