I'm going through one code, Its below. Here Runnable is an interface and implemented by Anonymous class and Interface's method is being implemented here. Upto here it sounds correct. But How this syntax is possible "new Runnable" ?
public class Library extends JFrame {
static Library frame;
private JPanel contentPane;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() { //Why its not giving error here ?
public void run() {
try {
frame= new Library();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
if I do like this
public interface MyInterface {
}
public class Test implements MyInterface{
public static void main(String[] args) {
MyInterface object = new MyInterface(); // Error Here "Cannot instantiate the type MyInterface" Its a right Error ...!
}
May I know please what is the difference between above two approaches ? Both are doing the same thing. Class is Implementing interface, Is there any difference ?
My Understanding say, Interface type can only reference to the objects of class which implements them. Is it correct ?
Can anyone help me to understand this ?