• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Leaving blank methods?

 
Ravaa Bal
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is leaving methods blank considered bad practice? For example in a preset Java interface, there may be a method 'close()' which you won't really need to use for the rest of the program, would it be okay to code it as:

public void close()
{
}

Thanks.
 
Maneesh Godbole
Saloon Keeper
Posts: 11073
13
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do you think the Adapter classes are implemented?
 
Abhijit Rai
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Although it is fine to leave a method blank ,it may be useful if you declare the method inside an interface and implement that interface in your class as blank method.
This way it would be clear that the method was meant to be implemented as a blank method rather than it being left blank by mistake .
 
Gavin Tranter
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might want to put a comment in the blank method to say that it is meant to be blank.
In somecases it might be useful to have the method throw an unimplemented exception if it is called accidentally.
 
Rob Spoor
Sheriff
Pie
Posts: 20559
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gavin Tranter wrote:You might want to put a comment in the blank method to say that it is meant to be blank.
In somecases it might be useful to have the method throw an unimplemented exception if it is called accidentally.


And the "unimplemented" exception is called java.lang.UnsupportedOperationException
 
Ravaa Bal
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow awesome, thank you all for your input
 
Gavin Tranter
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Rob
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic