Easy to Learn Java: Programming Articles, Examples and Tips

Start with Java in a few days with Java Lessons or Lectures

Home

Code Examples

Java Tools

More Java Tools!

Java Forum

All Java Tips

Books

Submit News
Search the site here...
Search...
 
Search the JavaFAQ.nu
1000 Java Tips ebook

1000 Java Tips - Click here for the high resolution copy!1000 Java Tips - Click here for the high resolution copy!

Java Screensaver, take it here

Free "1000 Java Tips" eBook is here! It is huge collection of big and small Java programming articles and tips. Please take your copy here.

Take your copy of free "Java Technology Screensaver"!.

Interfaces in Java: Very easy to read overview

JavaFAQ Home » General Java Go to all tips in General Java


Bookmark and Share

Interfaces in Java

In Java, a class can have at the most one immediate superclass. Multiple inheritance, where a class has more than one superclass, is not allowed in Java. However, one cannot ignore the importance of multiple inheritance because a large number of real-life applications require the use of multiple inheritance. Java provides an alternative approach to support multiple inheritance by introducing a new data type known as interfaces. Although a class cannot inherit multiple classes, it can implement more than one interface.

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 bodyThe 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/ 


 Printer Friendly Page  Printer Friendly Page
 Send to a Friend  Send to a Friend

.. Bookmark and Share

Search here again if you need more info!
Custom Search



Home Code Examples Java Forum All Java Tips Books Submit News, Code... Search... Offshore Software Tech Doodling

RSS feed Java FAQ RSS feed Java FAQ News     

    RSS feed Java Forums RSS feed Java Forums

All logos and trademarks in this site are property of their respective owner. The comments are property of their posters, all the rest 1999-2006 by Java FAQs Daily Tips.

Interactive software released under GNU GPL, Code Credits, Privacy Policy