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.
Besides its own class, which two groups of classes can use a protected member?
Protected equals default plus one more group.
Can a top-level class be declared protected? What does the compiler say?
Access levels such as protected and private describe members of a class.
Class A has a protected method. Which of these cannot call it?
Protected equals default plus subclasses.
A parent class declares protected void show(). Which override in a subclass is not allowed?
Which option is the narrowest?
Write a class Game with a public method play() that prints a heading and then calls three protected methods: start(), move() and finish(). Give Game simple versions of these methods. Then write subclasses Chess and Ludo that override the three protected methods with their own messages. In main, call play() on one object of each class. Explain why start(), move() and finish() are protected rather than private or public.
Put the order of the steps in the public method, and let each subclass decide what each step does.
Java's java.util.ArrayList has the protected method removeRange(from, to). Write a class RecentList that extends ArrayList of String and keeps only the five most recent items: override add so that, after adding, it removes the oldest items with removeRange whenever there are more than five. In main, add the eight entries Day 1 to Day 8 and print the list.
removeRange(0, k) removes the first k items; when the size goes over five, k is size() minus five.
6 more questions in this section. Enroll free to practise them all.
Enroll free