Q1. Can I have an interface inside a class in java?
Q2. Can I have an class inside an interface?
If yes, then in which situations should such classes/interfaces used.
Q1. Yes Q2. Yes.
Inside your class you may need multiple implementations of an interface, which is only relevant to this particular class. In that case make it an inner interface, rather than a public / package-private one
In your interface you can define some data holder classes that are to be used by implementations and clients.
One example of the latter:
public interface EmailService {
void send(EmailDetails details);
class EmailDetails {
private String from;
private String to;
private String messageTemplate;
// etc...
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With