Practice mode Java

Class B in package q extends class A in package p, and A declares protected int n. Inside an instance method of B, which expression fails to compile?

Correct answer B. new A().n

Explanation

Outside its package, a protected member can be accessed only through a reference whose type is the subclass doing the access, or one of its subclasses. The forms n, this.n and super.n all work on the current B object. The expression new A().n uses a plain A reference, so the compiler rejects it.

Report a problem

Answer checked How we write and check questions

Tip: press A, B, C or D to answer, and N for the next question.

Advertisement

Have a question about this answer?

Your email address will not be published. Required fields are marked *

Links and website addresses are not allowed in comments.