7

If I want to make a method in my interface optional, is it possible to do that ? How ?

Pan
  • 6,455
  • 5
  • 27
  • 27
  • 1
    What do you mean by *optional*? Should there be a default implementation, so that not everyone needs to implement it on their own? Or is it a method that need not work on every instance of this interface? – Joachim Sauer May 05 '11 at 15:05
  • possible duplicate of [Java equivalent to C# extension methods](http://stackoverflow.com/questions/4359979/java-equivalent-to-c-extension-methods) – Lukas Eder May 05 '11 at 15:07
  • How does the iterator interface allow remove to be optional ? Something along those lines. – Pan May 05 '11 at 15:08
  • 3
    It's only *optional by contract*, i.e. it's documented as such in the JavaDoc. Formally, it is not optional – Lukas Eder May 05 '11 at 15:09
  • Every Iterator has to have a remove() method but some throw `UnsupportedOperationException` – Peter Lawrey May 05 '11 at 15:19
  • Possible duplicate of [Optional Methods in Java Interface](https://stackoverflow.com/questions/10572643/optional-methods-in-java-interface) – Vikram Ojha Mar 19 '19 at 14:41

9 Answers9

7

Specify in the Javadoc that implementing classes may choose to throw UnsupportedOperationException.

artbristol
  • 32,010
  • 5
  • 70
  • 103
6

You can't.

What you can do, though, is create a second interface and move the "optional" method to that interface.

That way a class can chose to implement one or both of the interfaces.

Justin Niessner
  • 242,243
  • 40
  • 408
  • 536
  • 3
    Or indeed, have the second interface extend the first if its contract only sensibly exists in conjunction. – eggyal May 05 '11 at 15:05
  • one interface extending from other or two totally independent interfaces, the approach should be taken depending on requirements. both answers are good though. – Bhushan May 05 '11 at 15:11
4

You can not make a method optional in an interface, although you could do one of the following:

  1. Make your interface an abstact class, and implement only the "optional" method

  2. Implement the interface from you classes and throw NotImplementedException (or the like)

  3. Make the interface with an inner class which contains the optional method

Yet Another Geek
  • 4,251
  • 1
  • 28
  • 40
4

As other stated: you can't. But you can extend Interfaces, so you might construct something like:

public interface InterfaceA {
   void methodA();
}

public interface InterfaceB extends InterfaceA {
   void methodB();
}

This way you can use InterfaceA for implementing classes with only one specific method and InterfaceB when you can use both methods.

But of course, this all depends on your design.

Lucas de Oliveira
  • 1,642
  • 11
  • 16
2

No, you can not.

An interface is a contract, and must be implemented.

Mitch Wheat
  • 295,962
  • 43
  • 465
  • 541
1

You can make an abstract class of the same interface, that same class can the call the same methods but optionally.

Oliver Dixon
  • 7,012
  • 5
  • 61
  • 95
1

No, that's not possible. The closest you can get to not implementing a method in an interface is to immediately throw an exception. You should reconsider your type design.

Isaac Truett
  • 8,734
  • 1
  • 29
  • 48
1

you cannot do this 8) From What Is an Interface?

If your class claims to implement an interface, all methods defined by that interface must appear in its source code before the class will successfully compile.

Maybe you should use an abstract class.

Piccolomomo
  • 58
  • 1
  • 11
0

Not possible as far as i know.

Ian Jacobs
  • 5,456
  • 1
  • 23
  • 38