Saturday 23 March 2013

JAVA INTERFACES


Interface is a collection of method declaration and constants that one or more classes of objects will use.  Interface definition is same as class except that it consists of the methods that are declared have no method body.  They end with a semicolon after the parameter list.  Syntax for an interface is as follows.
            Syntax:            <access specifier> interface <it-name>
                                    {
                                                type varname1=value;
                                                type varname2=value;
                                                .
                                                .
                                                returntype method-name1(parameter-list);
                                                returntype method-name2(parameter-list);
                                                .
                                                .
                                    }

Here,
Ø  access specifier is public or not used.  public access specifier indicates that the interface can be used by any class.  Otherwise, the interface will accessible to class that are defined in the same package as in the interface.
Ø  interface keyword is used to declare the class as an interface.
Ø  it-name is the name of the interface and it is a valid identifier.
Ø  Variables declared inside the interface are implicitly final and static.  They cannot be changed in the implementing class.
Ø  All the methods in an interface are implicitly abstract methods.  Method implementation is given in later stages.
Ø  The main difference between a class and interface is class contains methods with method body and the interface contains only abstract methods.

            Example:         public interface shape
                                    {
                                                int radius=2;
                                                public void area(int a);
                                    }

No comments:

Post a Comment