public interface Printable { public void print(); } public class Printer { publi
ID: 3711817 • Letter: P
Question
public interface Printable {
public void print();
}
public class Printer {
public static void printInvoice(Printable p) {
System.out.println("Printing invoice");
p.print();
}
}
public class Order implements Printable {
public void print() {
System.out.println("Order object");
}
}
public class Rental extends Transaction implements Printable {
public void print() {
System.out.println("Rental object");
}
}
(Refer to code example given above). What happens when the following code is executed?
Printable p = new Rental();
Printer pr = new Printer();
pr.printInvoice(p);
A) “Printing invoice” is printed to the console.
B) “Printing invoice” and “Rental object” are printed to the console.
C) A runtime error occurs because printInvoice can’t accept Rental objects.
D) Nothing happens.
A) “Printing invoice” is printed to the console.
B) “Printing invoice” and “Rental object” are printed to the console.
C) A runtime error occurs because printInvoice can’t accept Rental objects.
D) Nothing happens.
Explanation / Answer
Option b correct.
“Printing invoice” and “Rental object” are printed to the console.
pr.printInvoice(p); // Here belongs to the Printable now control transfer to printableInvoice
public static void printInvoice(Printable p) {
System.out.println("Printing invoice"); / / It print Printing invoice
p.print(); // now control transfered to print function
public void print() {
System.out.println("Rental object"); // It print Rental object
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.