I'm working with a database and I create a public interface called Dao with extends from AutoCloseabe, so I have a class which implements this interface but I want to create some private methods there but they still need Autocloseable. So my question is, I can't create private methods in the interface without defining them in the interface. Which happens if I create a private method in the class but not overriding from Dao? They won't have autocloseable, will they?. And if not which solution can I implement?
-
Maybe you should consider some kind of factory pattern instead. The factory could expose only the public interface to the end user, but would allow it to create what ever instance you want to use internally ... as an idea – MadProgrammer Jan 20 '20 at 21:20
-
I'm not understanding you hehe, could you give me some example? – Guillermo Fuentes Jan 20 '20 at 23:39
2 Answers
The motivation behind the introduction in Java 9 of private methods in interfaces is for the same reasons you would use private methods in any other class body. It allows you to break up the code into reusable, manageable methods which are not inherited:
default public boolean tryHeads() {
return flip();
}
default public boolean tryTails() {
return !flip();
}
private boolean flip() {
return ThreadLocalRandom.current().nextBoolean();
}
This is grossly oversimplified. But see a similar question from me for more insight.

- 11,105
- 5
- 45
- 71
-
Mmm, I get you by doing in that way I will have to write the code of the method in the interface, won't I? And I need to write it in the class that implement my interface – Guillermo Fuentes Jan 20 '20 at 23:40
-
It depends on what you're attempting to achieve. A default method is good if the default implementation can either be reused by a subclass or doesn't even need to be reimplemented. If you follow a mixin design strategy, you'd use non-default methods to supply state to your default methods. – Rogue Jan 20 '20 at 23:43
-
In this sense, private interface methods aren't useful for the implementing subclasses. This is where there's advocacy for things like protected interface methods – Rogue Jan 20 '20 at 23:44
-
I mean, the main problem is that I have a Dao Interface which extends Autocloseable but I would like to make some private methods in the class which implements Dao, but I can't define them in Dao without writing the code there, and if I create them in my class they won't implement autocloseable – Guillermo Fuentes Jan 21 '20 at 00:27
Private methods don't make a lot of sense in an interface since the main idea of an interface is to provide an interface for objects to interact with eachother. Since private methods are not visible to other objects they can't use them to communicate and send messages.
If you would like to have private methods I would suggest you use an abstract class
instead.
As a sum up:
Q: So my question is, I can't create private methods in the interface without defining them in the interface.
Exactly that's the main point for interface
; they define a public API without caring about the internal implementation.
Q: What happens if I create a private method in the class but not overriding from Dao?
If you declare a method in an abstract class
and you don't override it two things may happen:
- The method was marked as
abstract
: you'll get a compile error saying that your implementation class doesn't satisfy the specification of the parent class.
1.1 If you declared a method in an interface and you don't implement it you will get the same error stating that your implementation class doesn't satisfy the interface it's trying to implement.
- The method was
public
and not marked asfinal
in theabstract class
: You may or may not override it. If you don't override it, the parent class implementation is going to be used; if you override it completely your child class code will be executed. If you call super.method() and then your implementation, both codes will be executed.

- 7,569
- 2
- 21
- 47
-
`private` methods do make sense in interfaces since the introduction of `default` methods in interfaces; see the other answer. – kaya3 Jan 20 '20 at 21:27
-
-
The question is not tagged `java-8`, and nothing in the question says it's about Java 8. – kaya3 Jan 20 '20 at 21:30
-
Nor is tagged `java-9^` but since it's the widest API adopted I assumed the person asking the question may be using that version instead. My apologies – Some random IT boy Jan 20 '20 at 21:32
-
Since the question is specifically about writing private methods in an interface, and the question is about best practices rather than how to solve a compilation error, it would be sensible to assume the OP is using a version of Java which allows private methods in interfaces. – kaya3 Jan 20 '20 at 21:36
-
If the OP is stating that he can't create private methods on an interface without declaring them there is very plausible to think he may be using a Java version equal or lower to 8. – Some random IT boy Jan 20 '20 at 21:40
-
*"I can't create private methods in the interface without defining them in the interface"* implies the OP *can* create them by defining them in the interface. – kaya3 Jan 20 '20 at 21:43
-
"Private methods don't make a lot of sense in an interface." What? Then why did they add them in Java 9. Sorry but your statement doesn't make a lot of sense. – neildo Jan 20 '20 at 21:50
-
BTW, they do make sense if you have 2 default methods that want to share code. – neildo Jan 20 '20 at 21:51
-
This was all covered in JSR 335; interfaces allowed for inheritance of behavior, and with that a whole slew of good and bad ideas were brought up (final, synchronized, etc in interfaces; all not done for good reason). Weaker accessibilities, however, have various use cases. package private and protected have their own challenges, but we may yet see those accessibilities on interfaces in the future. – Rogue Jan 20 '20 at 21:59
-
I would rephrase your answer. Private (and default) methods do not make sense in an interface, _unless_ that interface is already published, e.g. in a library that is used by others. If you would simply add another method you would break their implementations. Adding a default method (or multiple and maybe a private method they might both use) allows you to add another method without breaking existing implementations. – qutax Jan 21 '20 at 05:49