• Post Reply Bookmark Topic Watch Topic
  • New Topic

How can we create a concrete class by implementing only 1 method in an interface(with more methods)  RSS feed

 
Arnold king
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An Interface has ten methods(interface A{..ten methods})
i have to create a concrete class by implementing only one method
i don't want to give implementation for rest 9 method.
how can we do that..?
 
Tim Cooke
Marshal
Posts: 4048
239
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You cannot. If you implement an interface you have to provide implementations for all methods in that interface.

Why would you only want to implement one of the methods?

Edit: Technically this is true. You do have to provide implementations for all methods in an interface when you implement it. So the following is not allowed:


However, if you only want implement one of the methods now and come back for the rest later you can just write Stubs for them:

You have satisfied the Java compiler requirement that all methods must be implemented so you can create your concrete class, but you just have not provided a meaningful implementation for your problem domain which I assume you will be coming back to later.
 
James Boswell
Bartender
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Further to what Tim has said:

You can throw an UnsupportedOperationException for the methods you do not wish to implement. An example within the Java API is java.util.UnmodifiableList which throws these exceptions for operations such as add or remove.

However, as Tim stated, it seems strange that your implementing class only needs to implement one of the ten methods. Could it be the case that the single method being implemented should be in a separate interface?
 
Ivan Jozsef Balazs
Rancher
Posts: 999
5
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And why not an abstract class?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12564
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you write "...implements <interface>", you should think of it as signing a contract. You are saying "I swear that this class I am currently writing will implement ALL the methods defined in the interface" (assuming you are not creating an abstract sub-class).

So you are basically asking "How can I promise to implement all the methods and then NOT actually implement them all".
 
Campbell Ritchie
Marshal
Posts: 56562
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That lot might get past the compiler, bit it ain't programming. You have an interface which promises it will doThis and doThat. Now you are telling people you have implmeented that interface only to doThat.

The idea behind UnsupportedOperationException(←link) (UOE) is that some methods of some class in the collections framework, eg this one are described as optional. That means you can have a read‑only List, where the add() method simply throws that Exception. A lot of poeple use UOE for blocking particular calls…or it is useful if you have lots of methods and want to compile the class before all the methods are completed. But not as an excuse not to implement all non‑optional methods.
 
Arnold king
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ivan Jozsef Balazs wrote:And why not an abstract class?


Just because i want concrete class which can be instantiated,..
not abstract class (as we can't create object of an abstract class)
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is just no way to make a concrete class that implements only part of the methods of an interface. Either the class itself or its superclasses must implement all the methods of the interface.

Suppose it were possible to implement only one of the 9 methods of the interface, and someone would try to call one of the methods that are not implemented. What would you then expect to happen?
 
Tim Cooke
Marshal
Posts: 4048
239
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd still like to ask why you only want to implement one of the ten methods in the interface?
 
Jan Hoppmann
Ranch Hand
Posts: 147
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Cooke wrote:I'd still like to ask why you only want to implement one of the ten methods in the interface?


Perhaps the interface is a listener of some kind. When I use Swing and implement a MouseListener, but only want to handle double clicks, I have lots of methods, but only need to implement one.
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jan Hoppmann wrote:
Perhaps the interface is a listener of some kind. When I use Swing and implement a MouseListener, but only want to handle double clicks, I have lots of methods, but only need to implement one.

Thats exactly why we have adapter classes e.g. MouseListener -> MouseAdapter
From the API docs for MouseAdapter
An abstract adapter class for receiving mouse events. The methods in this class are empty. This class exists as convenience for creating listener objects.

 
Chan Ag
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An Interface has ten methods(interface A{..ten methods})
i have to create a concrete class by implementing only one method
i don't want to give implementation for rest 9 method.
how can we do that..?


This is a funny answer, but still an answer ( Given the kind of 'interesting' interview questions I have recently faced, this may 'in all fairness' be a not so bad thought process :-) )

Make the concrete class also extend from an abstract class that implements this interface and provides implementations for the remaining 9 methods.

Question - Is this an interview question?

Chan.



 
Arnold king
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all for your answers
Yes, it was an interview question!!
i don't know why the interviewer asked this question??
i answered in the similar way as your answers but only i didn't talk anything about UOE.
Thanks for introducing me about UOE,..
 
Campbell Ritchie
Marshal
Posts: 56562
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why didn't you say it was an interview question earlier?
That of course raises all sorts of other questions, including why do interviewers ask such daft questions? and would you want to work there anyway?


…and…You're welcome
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!