3
Interface Interface are syntactically similar to classes, but they lack instance variable, and their methods are declare without any body. That means you can define interfaces which don’t make assumptions about how they are implemented. Once it is defined, any number of classes can implement an interface. Also one class can implement any number of interfaces. To implement an interface, a class must create the complete set of methods defined by the interface . Interfaces are designed to support dynamic method resolution at run time. Interface adds most of the functionality that is required for many applications which would normally resort to using multiple inheritances in a language such as c++. Define an interface General form of an interface :---------------- access interface_name { return-type method-name1(parameter-list); return-type method-name1(parameter-list); type final- varname1=value; type final- varname1=value; //…………. return-type method- nameN(parameter-list); type final- varnameN=value; } Here access is either public or default. in default the interface is only available to another member of the package in which it is declared. when it is declared as public ,the interface can be used by any other code. Name is the name of the interface ,and can be any valid identifier. The methods which are declared have no bodies .they end with a semicolon after the parameter list. Each class that includes an interface must implement all of the methods. Variables can be declared inside the interface declarations. They are final and static, means they cannot be changed by the implementing class. All methods and variables are public if the interface, itself, is declared as public. A simple interface which contains one method called callme() that takes a single integer parameter. Interface callback { Void callback(int param); } Implementing interface

03_interface.doc

  • Upload
    phani

  • View
    1

  • Download
    0

Embed Size (px)

DESCRIPTION

int

Citation preview

InterfaceInterfacearesyntacticallysimilartoclasses, but theylackinstancevariable, andtheirmethodsaredeclarewithout any body. That means you can define interfaces which dont make assumptions about how they areimplemented. Onceit is defined, anynumber of classes canimplement aninterface. Alsooneclass canimplement any number of interfaces.To implement an interface, a class must create the complete set of methods defined by the interface . Interfacesare designed to support dynamic method resolution at run time.Interface adds most of the functionality that is required for many applications which would normally resort tousing multiple inheritances in a language such as c.Define an interface!eneral form of an interface"################access interface$name %return#type method#name&'parameter#list()return#type method#name&'parameter#list() type final#varname&*value) type final#varname&*value) ++,,,,.return#type method#name-'parameter#list() type final#varname-*value)./ereaccessiseitherpublicordefault. indefault theinterfaceisonlyavailabletoanothermemberofthepackage in which it is declared. whenit is declared as public ,the interface can be used by any other code.-ame is the name of the interface ,and can be any valid identifier. The methods which are declared have nobodies .they end with a semicolon after the parameter list. 0ach class that includes an interface must implementall of the methods.1ariablescanbedeclaredinsidetheinterfacedeclarations.Theyarefinal andstatic, meanstheycannotbechanged by the implementing class. All methods and variables are public if the interface, itself, is declared aspublic.A simple interface which contains one method called callme'( that takes a single integer parameter.Interfacecallback %1oid callback'int param().Implementing interface Onceaninterfacehas beendefined, oneor moreclasses canimplement that interface.toimplement aninterface, includetheimplements clauseinaclassdefinition, andthencreatethemethodsdefinedbytheinterface.access class classname2e3tends superclass4 2implements interface 2,interface,44% ++class#body . If a class implements more than one interface ,the interface are separated with a comma. If a class implement two interface that declare the same method, then the same method will be used byclients of either interface. The methods that implement an interface must be declared public. also , thetypesignatureoftheimplementingmethodmust matche3actlythetypesignaturespecifiedintheinterface definition. 5lass client implements callback ++implement callbacks interface % 6ublic void callback'int p( ++callback is declared using public specifier%7ystem.out.println'8callback called with9p().. :hen you implement an interface method ,it must be declared as public class client implements callback ++implement callbacks interface % public void callback'int p( ++callback is declared using public specifier%7ystem.out.println'8callback called with9p().void nonIfacemeth'( % 7ystem.out.println'8classes implement interfaces9()..Accessing implementations through interface reference/ere calls the callback'( method via an interface reference variable 5lass T; % 6ublic static void main'7tring as24(% 5allback c*new client'()c.callback'%public void test&'(%7ystem.out.println'AImplementing Test&A().public void test='(%7ystem.out.println'AImplementing Test=A().public void test?'(%7ystem.out.println'AImplementing Test?A()..class T7%public static void main'7tring as24(%@yclass 3*new @yclass'()3.test&'()3.test='()3.test?'()}}/*E:\>javac T7.javaE:\>java T7Implementing Test1Implementing Test2Implementing Test3*/