Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to hide method of parent interface in Java?

Tags:

java

oop

I have 3 classes.

public interface Operation {
    void move();
    void delete();
    void search(String criteria);
}

public abstract class AbstractOperationProcessor implements Operation {
    public void move() {
        // some logic
    }
}

public class DailyMailProcessor extends AbstractOperationProcessor{
    // need to hide this method because I don't want to provide them to customer
    public void delete() {}
    public void search(String criteria) {}
}

What I need is to hide methods delete() and search(String) from API. How can I do it without changing interface Operation and abstract class AbstractOperationProcessor?

like image 552
Alex Avatar asked Dec 07 '22 00:12

Alex


1 Answers

You cannot. The best you can do is implement stubs that throw something like NotImplementedException and document this fact.

I would use this as an opportunity to examine the definition of the top-level interface. If you need to hide some of its methods then the real problem may be that it aggregates unrelated functionality. You may need to split it into two separate interfaces.

Remember, you can "inherit" (i.e. implement) multiple interfaces.

like image 153
Jim Garrison Avatar answered Jan 12 '23 15:01

Jim Garrison