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

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...