Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding Extra method to interface

Tags:

interface

I have a tricky question on interface. Please try to give me a solution for it.

Scenario: I have written an interface with five methods. Also I have implemented more than 100 classes using this interface. Now, I need to add one more method to the interface. Consequently, I will need to define the same method in all classes. How can I avoid this???

Please reply...

Thanks, Akif

like image 536
Akif Avatar asked Sep 20 '10 11:09

Akif


People also ask

Can you add extra methods to interface?

An interface can extend any number of interfaces but one interface cannot implement another interface, because if any interface is implemented then its methods must be defined and interface never has the definition of any method.

How do you add methods to an interface?

Right-click the name of the interface. On the shortcut menu, choose Add > Add Method. In the Add Method wizard dialog box, provide the information to create the method as described in the next section. Select OK to add the method.

Can you extend methods in one interface to other interface?

An interface can extend other interfaces, just as a class subclass or extend another class. However, whereas a class can extend only one other class, an interface can extend any number of interfaces.

Can an interface have 2 methods?

No, its an error If two interfaces contain a method with the same signature but different return types, then it is impossible to implement both the interface simultaneously.


1 Answers

Could you avoid adding a method to the interface by instead creating a new interface which inherits from that first interface and then only changing the classes you need that new method on? Hence, if foo didn't need the new method, leave it alone but if bar did, change it to the new interface.

like image 176
wheaties Avatar answered Oct 12 '22 07:10

wheaties



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!