1

Hello so I am learning Java and I have a question puzzling me, some interfaces when we implement them and override their empty methods even if we put nothing in the override when we call the method it has an effect.

Some specific examples for that that confuse me are as follows,

  • Implementing the AutoCloseable interface and overriding its close() method, will allow us to close an object,

  • Same for Initializable interface if override it for JavaFx applications this will initialize our controls and in both cases we put nothing in the override yet the method have an action, I mean how ?!

In terms of code Here is the AutoClosable Interface

package java.lang;

public interface AutoCloseable {

    void close() throws Exception;
}

And when i implement it i do as follows

@Override
    public void close() throws Exception {

    }

then in another class i can use this close method in a try catch with resource as this

try(SecurityController security = new SecurityController()) {

} catch (Exception ex) {

} 

So here i was able to close the object even-though there not a single code in the interface or my implementation

More over here is the code for the Initializable interface

public interface Initializable {

    public void initialize(URL location, ResourceBundle resources);
}

and when i implement it i would do the following:

@Override
public void initialize(URL url, ResourceBundle rb) {     

}

and when i run the JavaFx application this method will initialize my controls even-though the method is empty in both my class and the interface, and i cannot work on the controls before this method finishes execution because it is what initializes my controls, but how does it do this while it's empty i don't get it ?!

I would be grateful if someone can explain this to me because it really confuse me so much

Zeyad
  • 537
  • 2
  • 7
  • 15
  • 1
    Please share some example [code](http://idownvotedbecau.se/nocode/) in the form of a [MCVE](http://idownvotedbecau.se/nomcve/) along with your expected result and actual result so that those who read your question can better help you. – D.B. Dec 16 '18 at 01:41
  • Your question is unclear. The "effect" that calling the empty method has is to call the empty method, and that's everything. – chrylis -cautiouslyoptimistic- Dec 16 '18 at 03:00
  • Ok i will add some code to illustrate – Zeyad Dec 16 '18 at 03:57

1 Answers1

1

Kind of by definition, when you implement an interface, instances of your class will gain the ability to do whatever that interface specifies.

AutoClosable has a close method. No matter what you actually write in the method implementation, you are always able to call close on an instance of AutoClosable. But what it actually does depends on your implementation.

So yes, implementing AutoClosable (or any other interface) with empty method bodies do have an effect - namely, becoming able to call close, and being able to use it in a try with resources statement. Note that this is only an ability to call a method. Does this mean that your object actually closes what it is supposed to close? No, because that depends on your implementation.

Sweeper
  • 213,210
  • 22
  • 193
  • 313
  • So you mean that by implementing the interface i can just call the close method since it exists in my class however it won't really close the object ? But what about the initialize method in the Initializable interface, this method really initializes the objects even though i leave it empty – Zeyad Dec 16 '18 at 03:56
  • @Zeyad I don’t know much about JavaFX. I suspect that it is some other parts of the library that is doing the initialisation. The library will call your method as well, so that you can implement some custom initialisation as well. – Sweeper Dec 16 '18 at 04:39
  • yes what you say make more sense, i just can't figure it out though. So back to the close() method, in this case when i call the close, it won't really close the object ! so it is useless in my example above ? – Zeyad Dec 16 '18 at 04:45
  • @Zeyad that’s right. It won’t do anything. However, your object might not need to be closed. An object only needs to be closed when it uses closable resources, like a file or a stream. – Sweeper Dec 16 '18 at 05:36
  • thank you so much for replying, so just for my information, if i keep instantiating allot of objects in my class, just normal objects not like a file or stream, wouldn't this impact the performance, this is why i wanted to get rid of them, i know garbage collection will eventually collect them but i was looking for a more guaranteed way. – Zeyad Dec 16 '18 at 05:39
  • @Zeyad you don’t need to worry about deleting or “closing” the objects you created. Garbage collector has been created for exactly this purpose. Just leave it to GC. – Sweeper Dec 16 '18 at 05:41
  • @Zeyad Are you still confused about something? If not, please consider accepting the answer. – Sweeper Dec 16 '18 at 07:04
  • I upvoted your answer because it is helpful, I'm just waiting if someone can explain how does the implementation of the initialize method work even if it's empty or what's behind that – Zeyad Dec 16 '18 at 15:09