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