Search the courses, subjects, tracks, games and features, or jump straight to a page.
5 cards from this section. Quick, good the night before a test.
Which keyword does a class use to adopt an interface, and which keyword does one interface use to inherit another interface?
Classes implement interfaces; interfaces extend interfaces.
In the interface interface Data { double PI = 3.142; double volume(); }, what modifiers does the compiler add to PI and to volume()?
Fields in an interface are constants; methods without a body are abstract.
Which declaration correctly makes Dog a subclass of Animal that also implements the interface Pet?
Extends first, implements second.
Which modifiers does Java automatically give to a field declared in an interface, such as double PI = 3.142?
Think of the mnemonic Please Stay Fixed.
An interface Conveyance has a constant FUEL_PRICE = 100.0 (rupees per litre, an assumed price) and a method double fuelCost(double km). A Bike runs 50 km on one litre and a Car runs 15 km on one litre. Write classes Bike and Car that implement Conveyance, and print the fuel cost of a 300 km trip for each, using an array of type Conveyance[].
Both classes can use FUEL_PRICE directly, because they inherit the interface's constant.
A school library allows 14 days for every item. Write an interface Borrowable with the constant MAX_DAYS = 14 and a method double fine(int daysLate). A Book is fined Rs 2 per late day; a Magazine is fined Rs 1 per late day but never more than Rs 10. Write a method static double totalFine(Borrowable[] items, int[] daysKept) and use it for a book kept 19 days, a magazine kept 29 days and a book kept 10 days.
Work out the days late for each item first, and let each object's own fine() method apply its rule.
6 more questions in this section. Enroll free to practise them all.
Enroll free