Friday, 9 May 2014

Con you implement an interface without using implements keyword

  • 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 implementskeyword)
    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();
    }

No comments:

Post a Comment