Given the content from the Tree.java and Plant.java files: and the code fragment: 9. Plant t = new Plant(); 10. /* insert code fragment here */ Which code fragment is valid at line 10?
An erratum to my previous answer, the answer is B as a protected method can be accessed from another package ONLY if it is a subclass. Answer A is also correct but B is more complete, just in case it is better to always mark the "most correct answer".
Answer is B.
To Test (attention to test in different packages and files, as stated):
----------------------------------------------------------------
package branch;
import root.*;
public class Plant extends Tree {
public static void main(String[] args) {
Plant t = new Plant();
t.m1(); // Answer A --- Ok, but incomplete.
t.m1(); t.m3(); // Answer B --- Ok
t.m1(); t.m3(); t.m4(); // Answer C --- Error
t.m1(); t.m4(); // Answer D --- Error
}
}
-------------------------
package root;
public class Tree {
public void m1() {}
private void m2() {}
protected void m3() {}
void m4() {};
}
----------------------------------------------------------------
The correct answer is A, the methods "m2" and "m3" cannot be seen by the child class because even inheriting it is in a different package, the same occurs for "m4", because when visibility is not placed, it is put automatically the "default" visibility which is even more restricted than private, so it cannot be seen by the "Plant" class, the only method that can be instantiated is "m1", because it is the only one seen.
upvoted 2 times
...
Log in to ExamTopics
Sign in:
Community vote distribution
A (35%)
C (25%)
B (20%)
Other
Most Voted
A voting comment increases the vote count for the chosen answer by one.
Upvoting a comment with a selected answer will also increase the vote count towards that answer by one.
So if you see a comment that you already agree with, you can upvote it instead of posting a new comment.
iSnover
Highly Voted 2 years, 3 months agocarloswork
Highly Voted 2 years, 2 months agoMPignaProTech
Most Recent 2 months, 2 weeks agoiSnover
2 years, 3 months ago