Monday, April 23, 2018

Can we create an instance of an interface ?

  1. interface MyInterfaceExe {
  2. MyInterfaceExe exe = new MyInterfaceExe();
  3. }
No ! we cannot instantiate an interface. Compiler will give an error saying that cannot instantiate the type of  MyInterfaceExe.
  1. interface MyInterfaceExe {
  2. MyInterfaceExe exe = new MyInterfaceExe(){ };
  3. }
Yes ! we can create the object of an interface using anonymous class as above.

No comments:

Post a Comment

Blueprint for self-improvement

To learn faster: Make the process fun To understand yourself : Write To understand the world better : Read To build deeper connection : Lis...