QuestionQ42

Working with Inheritance

Given the following code fragment:

Question Image

Which two independent modifications allow the code to compile?

Choose two
  • A Make the method at line n1 public.
  • B Make the method at line n2 public.
  • C Make the method at line n3 public.
  • D Make the method at line n3 protected.
  • E Make the method at line n4 public.
Explanation

An overriding method cannot reduce the access level of the inherited method. Earth.revolve() has package-private access, which is weaker than the protected access of Planet.revolve(). Declaring Earth.revolve() as either public or protected satisfies the access requirement. Earth.rotate() is already valid because protected is broader than package-private access.

Community Discussion

No comments yet. Be the first to start the discussion!