Home | Java - interface

Chapter: Web or internet Programming : JAVA

Java - interface

Using the keyword interface, you can fully abstract a class’ interface from its implementation. That is, using interface, you can specify what a class must do, but not how it does it. Interfaces are syntactically similar to classes, but they lack instance variables, and their methods are declared without any body.

 

INTERFACE

 

Using the keyword interface, you can fully abstract a class’ interface from its implementation. That is, using interface, you can specify what a class must do, but not how it does it. Interfaces are syntactically similar to classes, but they lack instance variables, and their methods are declared without any body.

 

To implement an interface, a class must create the complete set of methods defined by the interface

 

This is the general form of an interface:

 

access interface name {

 

return-type method-name1(parameter-list);

 

return-type method-name2(parameter-list); type final-varname1 = value;

 

type final-varname2 = value;

 

// ...

 

return-type method-nameN(parameter-list); type final-varnameN = value; }

 

 

When no access specifier is included, then default access results, and the interface is only available to other members of the package in which it is declared. When it is declared as public, the interface can be used by any other code. In this case, the interface must be the only public interface declared in the file, and the file must have the same name as the interface.

 

 

Variables can be declared inside of interface declarations. They are implicitly final and static, meaning they cannot be changed by the implementing class. They must also be initialized. All methods and variables are implicitly public.

 

 

interface Callback {

 

void callback(int param);

 

}

 

 

Implementing Interfaces

 

Once an interface has been defined, one or more classes can implement that interface. To implement an interface, include the implements clause in a class definition, and then create the methods defined by the interface. The general form of a class that includes the implements clause looks like this:

 

 

class classname [extends superclass] [implements interface [,interface...]] { // class-body }

 

 

When you implement an interface method, it must be declared as public

 

package day1;

 

interface callback{ void callmenow();

}

 

class client implements callback{ public void callmenow(){

System.out.println("this is an implementation of interface");

 

}

 

 

}

public class interfaceexample {

 

public static void main(String args[]){

 

// creatinf an obj for interface through the method class //implements its method

 

callback c=new client(); c.callmenow();

}

 

}

 

 

Multiple Inheritance In JAVA

 

interface callback1{ void callmenow1();

}

 

interface callback2{

 

void callmenow2(int x);

}

 

class client2 implements callback1,callback2{ public void callmenow1(){

 

System.out.println("this is an implementation of interface");

}

 

public void callmenow2(int x){

 

System.out.println("Intrger passed in the second implementaion is" + x);

 

}

 

}

 

public class multipleinheritance { public static void main(String args[]){

 

callback1 firstobj=new client2(); callback2 secondobj=new client2();

 

firstobj.callmenow1(); secondobj.callmenow2(23);

 

}

 

}

Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail
Web or internet Programming : JAVA : Java - interface |


Privacy Policy, Terms and Conditions, DMCA Policy and Compliant

Copyright © 2018-2024 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.