224k views
3 votes
Which of the following statements is false?

A. A public class can be accessed by a class from a different package.
B. A private method cannot be accessed by a class in a different package.
C. A protected method can be accessed by a subclass in a different package.
D. A method with no visibility modifier can be accessed by a class in a different package.

User Akamike
by
8.3k points

1 Answer

1 vote

Final answer:

The false statement is that a private method cannot be accessed by a class in a different package.

Step-by-step explanation:

The false statement is B. A private method cannot be accessed by a class in a different package. A public class (with the 'public' access modifier) can be accessed by a class from a different package. For example, if you have a public class called 'MyClass' in package A, you can access it from a class in package B by importing package A and creating an object of 'MyClass'. A protected method (with the 'protected' access modifier) can also be accessed by a subclass in a different package. However, the subclass needs to inherit the protected method from its superclass. The protected method cannot be accessed directly from other classes in a different package. A method with no visibility modifier (also known as the 'default' visibility) can be accessed by a class in the same package, but not by a class in a different package.

User Ofer Magen
by
7.4k points