maregor
maregor

Reputation: 797

Public/private method signatures in Java interfaces

This is a homework question about the comparing interfaces and classes in Java.

I know that interfaces don't have method implementation or instance variables but classes do.

But is it true that Java interfaces only have public method signatures whereas in classes you have have both public and private method signatures when you implement the interface?

This resource seem to make me think so.

Upvotes: 2

Views: 827

Answers (1)

wadda_wadda
wadda_wadda

Reputation: 1004

Interfaces don't have private members.

"The Java programming language provides mechanisms for access control, to prevent the users of a package or class from depending on unnecessary details of the implementation of that package or class."

Access control is all about masking implementation details. Interfaces have no implementation (excluding default methods, which were introduced in Java 8).

Conversely, a class can have public, private, or protected members since a class is implementation code.

That said, this is due to change in Java 9 when private members are going to be allowed in interfaces.

Upvotes: 2

Related Questions