Sergey Shustikov
Sergey Shustikov

Reputation: 15821

Java Interface\abstract class constriction

Today, our team has the problem.

There is a class AClass that implements the interface AInterface. To date, we need to introduce a new entity(BClass) that would use only part of the interface A.

enter image description here

The first thing about which we think - split interface AInterface into 2 components (composition)

enter image description here The problem is that the logic AClass->AInterface - is a model prom pattern MVC. And we extremely do not want to cut it into several interfaces.

We know that Java provides a mechanism for inheritance to extend a class or interface.

But is there any way to constrict the implementation? Or maybe exist another way?

Note : we doesn't want use UnsupportedMethodException. Our goal - clean API.

Update :

Next solution - not for us.

enter image description here

GOAL :

enter image description here

Upvotes: 0

Views: 194

Answers (5)

einschnaehkeee
einschnaehkeee

Reputation: 1888

You cannot "disable" polymorphism in certain cases, it's a major feature of the Java language.

If BClass shouldn't have those methods, then it shouldn't implent the interface.

AClass does more than BClass, so it should be another type. Why would you want them to be interchangeable?

On another note, many libraries use UnsupportedMethodException (like even the Java SDK with List collections). It just needs to be documented properly. So if you need ro use that to achieve your goal, go for it.

Upvotes: 1

OldCurmudgeon
OldCurmudgeon

Reputation: 65849

Your needs seem a little strict but perhaps a abstract class could help.

public interface AInterface {

    public void add();

    public void remove();

    public void invalidate();

    public void move();
}


public abstract class BBase implements AInterface {
    @Override
    public abstract void add();

    @Override
    public abstract void remove();

    @Override
    public void invalidate() {};

    @Override
    public void move() {};

}

public class BClass extends BBase {

    @Override
    public void add() {
    }

    @Override
    public void remove() {
    }

}

Here I create a BBase which stubs out the two methods you want removed but leaves the other two abstract. BClass demonstrates how it would be used.

Upvotes: 0

Oleksandr Kelepko
Oleksandr Kelepko

Reputation: 234

You can do this if you compile AClass and BClass separately. I.e. compile AClass with the full version of the interface, then modify the interface (remove the methods) and compile BClass with this modified version of the interface. P.S. By no means this is a painless approach.

Upvotes: -1

V_Singh
V_Singh

Reputation: 759

The very fact that you have a usecase which only requires half of the methods exposed in the original interface tells you that you can further break that interface down. If you think about the design - how do your objects behave in your usecase scenarios, will tell you how it should be designed.

Just by looking at the names of the methods you have given, I'd expect them to be 2 different interfaces where AClass implements both the interfaces while BClass only implements the second interface.

Upvotes: 2

Ernest Friedman-Hill
Ernest Friedman-Hill

Reputation: 81724

Put your restricted subset into one interface, and have the larger interface extend it. Then have A implement the child (larger) interface, and B implement the parent (smaller) one. Then both A and B implement the smaller interface, while only A implements the larger. Use the smaller interface for coding to whenever you can.

public interface AInterface {
    void add();
    void remove();
}

public interface ASubInterface extends AInterface {
    void invalidate();
    void move();
}

public class AClass implements ASubInterface { /* 4 methods */ }
public class BClass implements AInterface { /* 2 methods */ }

Upvotes: 4

Related Questions