6

Is it possible for an interface to be accessible only in the same package and child packages?

I have defined an interface with 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 child package where I want to define a class which 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 resolved to a type

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

Thanks

srh
  • 1,661
  • 4
  • 30
  • 57

4 Answers4

12

In Java there is no such thing as a "child package". Don't get fooled by the dots. com.mycompany.myapp.dao and com.mycompany.myapp.dao.jpa are two separate packages which have no relation to each other.

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

Nathan Hughes
  • 94,330
  • 19
  • 181
  • 276
Hoopje
  • 12,677
  • 8
  • 34
  • 50
  • 1
    And do not forget to `import com.mycompany.myapp.dao.BaseDao;` within `com.mycompany.myapp.dao.jpa.BaseDaoJpa`. You need to import it because - as @Hoopje explained - the packages are unrelated. – Turing85 Jun 19 '15 at 14:47
  • So there is no concept of package hierarchy in Java? Although if you look at them physically in Windows Explorer for example, you can see the hierarchy. – srh Jun 19 '15 at 14:57
  • I believe the question is *Why can't I access my interface in a different package* rather than *Can you restrict an interface to child packages* – Chetan Kinger Jun 19 '15 at 15:03
  • @ChetanKinger That's not what the OP wrote. – DJClayworth Jun 19 '15 at 15:15
  • @DJClayworth Sure. But what the OP wan'ts to know the reason behind the error. That's the actual question. – Chetan Kinger Jun 19 '15 at 15:16
4

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

In the table you can see that default or No modifier is limited to acess only by the same class or other classes in the same package. As i understand you want it to be visible to other packages as well but not to the world, and for that you need to use protected modifier, but it is impossible since it is not aplicable so going back to your question no you cant :- (

fill͡pant͡
  • 1,147
  • 2
  • 12
  • 24
  • 1
    thanks. `protected` modifier for interface is exactly what I need but java does not support it; don't know why? – srh Jun 19 '15 at 14:59
  • It has no reason to exist xD. EDIT: Have a look here: http://stackoverflow.com/questions/3869556/why-a-class-cannot-be-defined-as-protected – fill͡pant͡ Jun 19 '15 at 15:18
1

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

Community
  • 1
  • 1
TrapII
  • 2,219
  • 1
  • 15
  • 15
0

It looks like you have extends where you need implements.

public class BaseDaoJpa<T, Id extends Serializable> implements BaseDao<T, Id>    
{
...
}
slugmandrew
  • 1,776
  • 2
  • 25
  • 45