Saturday, November 6, 2010

Interfaces in Java, What are interfaces in java, java interface questions

Interfaces in Java, What are interfaces in java, java interfaces questions, interface in java

  1. An interface is a group of related methods with empty bodies. eg.
    • interface example{
    • void A(int a); // method A
    • void B(int b);//empty  methods
    • }
  2. It introduces a new reference type whose members are classes, interfaces, constants and abstract methods. This type has no implementation, but otherwise unrelated classes can implement it by providing implementations for its abstract methods.
  3. A nested interface is any interface whose declaration occurs within the body of another class or interface. A top-level interface is an interface that is not a nested interface.
  4. A class may be declared to directly implement one or more interfaces, meaning that any instance of the class implements all the abstract methods specified by the interface or interfaces. A class necessarily implements all the interfaces that its direct superclasses and direct superinterfaces do. This (multiple) interface inheritance allows objects to support (multiple) common behaviors without sharing any implementation.  
  5. Interface Modifiers
    • Allowed modifiers are
    • InterfaceModifier: one of public, protected, private, abstract, static, strictfp
  6. abstract Interfaces - Every interface is implicitly abstract. This modifier is obsolete and should not be used in new programs.
Not all modifiers are applicable to all kinds of interface declarations. The access modifiers protected and private pertain only to member interfaces within a directly enclosing class declaration. The access modifier static pertains only to member interfaces. A compile-time error occurs if the same modifier appears more than once in an interface declaration.