Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Interface

 
Ingoba Ningthoujam
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How to implement selected methods of an interface?
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15281
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ingoba, can you explain your question in more detail - what do you mean exactly?

Have a look at The Java Tutorial:
Lesson: Interfaces and Inheritance
 
Ingoba Ningthoujam
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your concern. I will elaborate my question.

I defined one interface having three methods. I want to implement only two methods of this interface in a class.How can it be done? Please reply.
 
Ådne Brunborg
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Either

1) Define a different interface to implement. Remember, interfaces can extend other interfaces, and in this case you could define two methods in one interface and the third in a subinterface of that.

or

2) Implement the method to throw java.lang.UnsupportedOperationException:



If your class "implements" an interface, you must implement all methods that interface requires.
[ December 06, 2006: Message edited by: �dne Brunborg ]
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15281
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can write a class that implements the interface, but if the class does not implement all methods of the interface, the class has to be an abstract class.

If you don't want the class to be abstract, then you must implement all the methods of the interface. If you don't know what to do with the third method, you can provide an implementation that for example throws an UnsupportedOperationException like �dne shows, but that may not be what you want.
 
Sanjit Kumar
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adne has given the best explanation of this question.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic