Java – can I access interfaces only in the same package and sub package?
•
Java
Can I access interfaces only in the same package and sub package?
I have defined an interface using the default 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 sub package. 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 got this mistake:
So is this Java's limitation on the interface, or am I wrong?
thank you
Solution
In Java, there is no such thing as "sub package" Don't be deceived by embellishment com. mycompany. myapp. Dao and com mycompany. myapp. dao. JPA is two independent packages that are independent of each other
So answer your question: No, it's impossible to provide interfaces only for sub packages You can expose your interface, but then it will be visible to all other packages
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码