Monday, April 23, 2018

Can we have a constructor in an interface ?

  1. interface MyInterfaceExe {
  2. public void MyInterfaceExe (){
  3. // code here
  4. }
  5. }
No ! we cannot have a constructor in an interface. Compiler will give an error. 

  1. interface MyInterfaceExe {
  2. public static void MyInterfaceExe (){
  3. // code here
  4. };
  5. }
       ----------------------------------------------------
  1. interface MyInterfaceExe {
  2. public default void MyInterfaceExe (){
  3. // code here
  4. };
  5. }
In above case, Yes ! we can have a constructor only if we have either static or default keyword in constructor bodies ending with (;) semicolon.

No comments:

Post a Comment

How to run standalone mock server on local laptop

 Please download the standalone wiremock server from Direct download section at the bottom of the page.  Download and installation Feel fre...