Search code examples
javainterfaceimplements

Without Implement Keyword, Can we use interface?


Can We Use Variable and methods of Interface without using Keyword 'Implements'.

Note:Interface and Classes are in same Package.

Thanks in Advance..!!!


Solution

    • All variables of an interface are public static final by default, so you can directly use them
    • You can implement an interface by means of anonymous class (without using implements keyword)

      public static void main(String[] args) throws Exception{
         System.out.println(I.s); // accessing Interface I's variable
         I i = new I() {
      
          @Override
          public int getS() {
              return 10;
          }
         };
         System.out.println(i.getS()); // accessing I's method
      }
      
      
      interface I {
      
         String s = "test";
      
         int getS();
      }