1

I am using an interface in my code which has been there for a long time and many classes have implemented it. Now i have to add a new method to this interface for a new class [old classes dont need the new method]. So as has been suggested at many places that i can extend that old interface and create a new one with new method. Now my problem is that app launcher which uses the interface implementation is only having reference to base interface and using base interface i can't call method in new interface.

baseInterface is extended by newInterface

Class Applauncher{

  baseInterface b;

}

So as can be seen i can't call new method in "newInterface" in Applauncher class.

I want a solution which will not shake up my old implementation.

7
  • 2
    Why do you need to add this method in your interface if there is only one class using it? Commented Mar 8, 2013 at 12:38
  • At present just one and it has to implement baseInterface + new method. Commented Mar 8, 2013 at 12:38
  • The question is a bit confusing - are you saying that your Applauncher class cannot be changed, and holds a reference to baseInterface - but the actual reference being stored is to a newInterface? If so, I think you might be able to handle your problem with casting. So newInterface asNew = (newInterface) b, where b is a variable of type baseInterface which actually stores a reference to newInterface. Commented Mar 8, 2013 at 12:39
  • Why don't you simply add the method you need to the new class without adding it to the interface? Commented Mar 8, 2013 at 12:44
  • Yes Applaunch class is not supposed to be changed. I need to amke a call "newInterface.newMethod()" in AppLauncher but with current implementation i can't do that. You are doing down casting i.e base class to child class, java should throw exception, isn't it? Commented Mar 8, 2013 at 12:45

3 Answers 3

6

Basically at the point where you need to call the new methods in your applauncher, you could do something like this:

if (b instanceof NewInterface) {
  ((NewInterface)b).newMethod();
}

Would that be a solution for you?

Sign up to request clarification or add additional context in comments.

2 Comments

I agree, i would look for a more neat solution but if i dont get any other way then your approach will suit me most.
Yeah, this is not a solution you want to use too much, but in cases like yours it makes sense...
4

Admittedly not yet released, but when Java 8 comes out you'll have a nice solution to this general issue:

public interface MyInterface {
   Calendar myMethod();
   Object myMeth2() default null;
}

The important point there is the default null bit - the default keyword has a new usage from 8 onwards which allows you to specify default return values (meaning you don't need to implement this method in all implementations.)

Comments

0

Java 8 on wards provides a facility to create default methods inside the interface. Methods which are defined inside the interface and tagged with default are known as default methods. These methods are non-abstract methods. They allow adding new methods to interfaces without breaking the interface for those that already implement it.

interface MyInterface {    
    // default method    
    default void method1(){    
        System.out.println("Hello, this is default method");    
    }    
    // Already existing Abstract method    
    void method2(String msg); 
}   

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.