Java interface


General J2EE: Java interface

  1. Java interface (2 messages)

    Can anyone please advise on Below question, I was recently asked this question on a Java Interview

    Suppose you have an interface say XYZ, which has been implemented by dozens of classes. Now you need to add another method in that interface XYZ. What would be your approach to make that change in minimal way to solve the problem of overriding that method in all implementing classes?

    Threaded Messages (2)

  2. Java interface[ Go to top ]

    Rajesh, an pre-defined interface is practically unchangeable -- once published, any modification on the interface, for instance by adding a method, will break all the clients. So, in my mind, the best way of handling this challenge is to declare that new method in a different, new interface and let only the classes that require the new method implement the new interface, too.
    The best way of tackling the problem -- in general -- is to use abstract classes from the outset.
    Jave says that with Default/Defender methods coming in version 8, you will be able to assign a default implementation of a given method in an interface.  

  3. Re: Java Interface[ Go to top ]

    As sarah Sokolov said, you should create a new interface for the new method. The class which requires the method should implement the new interface.