Website Design United States, Website Design California, Website Designing United States, Website Designing California

Interfaces in Java

An interface declaration in Java introduces a new data type. An interface is a named collection of abstract methods and constants. Interfaces are syntactically similar to classes, except that they contain only abstract method declarations and constants. They are defined with a source code file with the .java extension that is compiled to a class file in the same way as classes. In Java, interfaces provide a convenient alternative to multiple inheritance.

The body of an interface declares a set of methods but does not provide their implementations. A class that implements an interface has a legal binding to implement all the methods defined in the interface, thereby agreeing to a certain behavior. A class can be declared to implement any number of interfaces by using the keyword implements followed by one or more interfaces.

Defining an Interface

An interface is defined using the syntax given below:

access_modifier interface interface_name extends Super_interfaces{
return_type method1(parameter_list);
.
.
.
return_type methodn(parameter_list);
datatype variable1 = value;
.
.
.
datatype variablen = value;}

The syntax defined above has two components:

the interface declaration, and the body of the interface. They are discussed in the following sections:

The interface declaration

An interface declaration consists of the following components:

·access_modifier: An interface can be declared by using the public access modifier or without any access modifier. When an interface is declared as public, it can be accessed by any class in any package. If no access modifier is used in the declaration, the interface is accessible only within the package in which it is declared.

·interface interface_name: The keyword interface in the declaration tells the compiler that it is an interface declaration and name of the interface is the one that follows the interface keyword. The name of an interface must be a valid identifier.

·extends Super_interfaces: An interface can extend other interfaces in the same way as a class extends another class. The only difference between these two is that where a class is allowed to extend only one class, an interface can extend any number of interfaces. The extends keyword in an interface declaration tells the compiler that the interface extends the list of interfaces that follows the extends keyword.

The interface body

The interface body contains method declarations for all the methods included in the interface. A method declared within an interface does not have a body, i.e., the method declaration ends with a semicolon. This is because an interface does not provide implementations for the methods declared within it. Apart from method declarations, an interface contains constant declarations.

All the constants defined in an interface are by default static and final. Therefore, there is no need to explicitly use these keywords to declare variables inside the interface body. All methods and variables in an interface are implicitly public if the interface is declared as public. It is also not necessary to declare the methods as abstract as well.

Member declarations in an interface disallow the use of some declaration modifiers, i.e., the following modifiers cannot be used to declare members in an interface:

·transient
·volatile
·synchronized

Also, the private and protected access modifiers cannot be used to declare members of an interface. The following example demonstrates how to define an interface:

public interface MyInterface{
void method1();
int method2(int k);
int x = 1;
int y = 2;
}

Implementing Interfaces:

When an interface has been defined, any number of classes can implement it. A class implements an interface by using the implements keyword followed by the interface name in its declaration part. A class that is declared to implement an interface provides definitions of all the methods declared in the interface. The following example shows the structure of a class named F0014 that implements the java.lang.

Runnable interface:

public class F0014 implements Runnable{
public void run(){
//statements }
}
The Runnable interface is defined in the java.lang package. This interface declares a single method with the following signature:

public void run();

The F0014 class implements it by using the implements keyword followed by the interface name, and then provides the definition of the run() method with the following signature:

public void run(){
}
The run() method implemented by the class must be declared as public with a return type of void or else a compile-time error will occur.A class can implement any number of interfaces at a time. Considering this case, if the F00014 class wants to implement more than one interface, a list of interfaces can be put after the implements keyword with a comma in between two interfaces.

Partial implementations of an interface by a class:

If a class implements an interface but does not provide implementations for all the methods defined in that interface, that class must itself be declared as abstract or else a compile-time error will occur.
For example,
abstract class F0014A implements MyInterface{
int s, t;
public void method1(){
}
}

Here, the class does not implement all the methods declared in the MyInterface interface (defined earlier) and hence is declared as abstract.

An interface can extend another interface:

An interface can inherit another interface by including in its declaration the extends keyword followed by interface name (to be inherited) in the same way as a class extends another class. When a class implements such an interface (i.e., an interface that extends another interface), it must provide implementations for all the methods defined in the interface as well as methods defined in the superinterface.

About the Author:

uCertify was formed in 1996 with an aim to offer high quality educational training software and services in the field of information technology to its customers. uCertify provides exam preparation solutions for the certification exams of Microsoft, CIW, CompTIA, Oracle, Sun and other leading IT vendors. To know more about uCertify, please visit http://www.ucertify.com/

Author Information:

Anonymous

Comments:

Add your comments here.

Name

Comment

You can also send feedback to feedback@programmers-corner.com

There are currently no comments available.

 















 


© 2008-2009 dotnet4all.com