*
The moose likes Java in General and the fly likes Interface Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Interface" Watch "Interface" New topic
Author

Interface

Ingoba Ningthoujam
Ranch Hand

Joined: Dec 04, 2006
Posts: 90
How to implement selected methods of an interface?
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

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


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Ingoba Ningthoujam
Ranch Hand

Joined: Dec 04, 2006
Posts: 90
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

Joined: Aug 05, 2005
Posts: 208
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 ]

Entia non sunt multiplicanda praeter necessitatem
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

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

Joined: Dec 04, 2006
Posts: 35
Adne has given the best explanation of this question.
 
wood burning stoves
 
subject: Interface
 
Similar Threads
Home interface or Home Interface stub
Time Capsule
Writing an Interface
How To Call Two Different Classes From the Main Program Using the Same API?
Regarding Interfaces