BugShotGG
BugShotGG

Reputation: 5190

how to properly change an interface after it has been implemented by many classes? java

I have an

public interface DoIt {
    void dosomething (int i, double x);
    int dosomethingelse (String s);
}

some class that implement it is class_of_a etc... and i want to add an new method in the interface lets say diditwork(int x); How am i suppose to do that while avoiding the problems of recompiling or whatever problem that might be ? What would be the new hierarchy ?

Upvotes: 3

Views: 138

Answers (3)

Andrzej Bobak
Andrzej Bobak

Reputation: 2112

create another interface and let the given class implement both interfaces

e.g.

public interface DoIt { 
    void dosomething(int i, double x);
    int dosomethingelse(String s);
}

and new interface

public interface CheckIt {
    boolean diditwork(int x);
}

classes without necessity to check would implement only DoIt interface, classes with necessity to check would implement also CheckIt interface

Upvotes: -1

Simulant
Simulant

Reputation: 20112

you can extend your existing Interface like this:

interface DoItMore extends DoIt {  diditwork(int x);  }  

so you will have your old interface for low level Classes and your new Interface for high level. Then you have to change the used Interface in your High level class.

Upvotes: 2

Eng.Fouad
Eng.Fouad

Reputation: 117587

Create a new interface and extend the old interface, something like:

interface DoIt2 extends DoIt
{
    // void doSomething(int i, double x);
    // int doSomethingelse(String s);
    void didItWork(int x);
}

Upvotes: 7

Related Questions