Is it possible for an interface to be available in only one package and child packages?

Is it possible for an interface to be available in only one package and child packages?

I defined an interface with a standard modifier:

package com.mycompany.myapp.dao; import java.io.Serializable; interface BaseDao<T, Id extends Serializable> { public void create(T t); public T readById(Id id); public void update(T t); public void delete(T t); } 

Now I have a child package where I want to define a class that implements BaseDao . So I wrote this code:

 package com.mycompany.myapp.dao.jpa; import java.io.Serializable; public class BaseDaoJpa<T, Id extends Serializable> implements BaseDao<T, Id> { ... } 

But I get this error:

BaseDao cannot be allowed for type

So is this a Java restriction for an interface, or am I doing it wrong?

thanks

+5
source share
4 answers

In Java, there is no such thing as a "child package". Do not be fooled by the dots. com.mycompany.myapp.dao and com.mycompany.myapp.dao.jpa are two separate packages that have nothing to do with each other.

So, to answer your question: no, it is impossible to make the interface visible only to child packages. You can make your interface public, but then it will be visible to all other packages.

+10
source

Take a look at Java Acess Modifiers : https://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html

In the table you can see that by default or Without a modifier is limited to only the same class or other classes in one package. Since I understand that you want it to be visible to other packages, but not to the whole world, and for this you need to use a protected modifier, but this is not possible, since this is unacceptable, so go back to your question, you cannot: - (

+4
source

If you are looking for some way to hide and provide only what you want for the rest of your Java application, you might need a component, so take a look at OSGi. This question is a good start to reading before jumping (or not) into it.

+1
source

It looks like you have extensions where you need tools.

 public class BaseDaoJpa<T, Id extends Serializable> implements BaseDao<T, Id> { ... } 
0
source

All Articles