Home Java Example Java Core Java Interface
Questions:Ask|Latest

 
 

Share on Google+Share on Google+

Java Interface

Advertisement
In this section we will discuss about the interface in Java.

Java Interface

In this section we will discuss about the interface in Java.

Objective of this tutorial

After reading this tutorial your query will be end for :

1. Can an interface implements another interface ?
2. Can an interface extends the other interface.
3. How a class can implement one or more than one interfaces.

Interface in Java is created for referencing something to other. To create an interface in Java the keyword "interface" is used. In Java interface may or may not contains the constants (constants defined inside the interface are implicitly public static final), methods signature (all the methods declared inside the interface are implicitly public and abstract). We can't create an object of interface. Interface can be implemented by a Java class. To implement an interface the keyword "implements" is used. An interface can't implement another interfaces rather it can only extend the another interfaces using the keyword "extends". A Java class can implement more than one interfaces by separating them with a comma. For Storing and Naming the interface programmer follows the conventions like a Java class. It is saved with the .java extension. Inside an interface we can't define the body of method, we can't use the curly braces with the declared methods. We can only declare/give the signature of method which will be closed by following a semicolon. Methods inside the interface can only be a public and we can not declare it as final because, final methods can't be inherited by its subclasses or implementing classes. The public interface can be implemented by any class in any package.

Declaration of interface

An interface cab be declared as follows :

accessSpecifier interface InterfaceName {
   // declare constants (public static final)
   // declare methods (public)
}

Declaration of interface for extending one or more interfaces.

accessSpecifier interface InterfaceName extends interface1, interface2, interface3{
   // declare constants (public static final)
   // declare methods (public)
}

Example

Here I am giving a simple example which will demonstrate you about how to write and use an interface in Java. In this example we will see you the various aspects of interface such as why an interface can't implement another interface, how an interface can extend the another interface as well as we will see that how a class can implement one and more interfaces.

Suppose I have created an interface and compiled that interface, it will be compiled successfully like below :

JavaInterface.java

public interface JavaInterface
{
    public void display();
}

But, if I created an another interface and implements the JavaInterface.java like below, then the compiler will generate an error.

public interface TestInterfaceImplementation implements JavaInterface
{
    public void show();    
}

But, if we extends the JavaInterface.java into TestInterfaceImplementation.java then it will be compiled successfully like below :

public interface TestInterfaceImplementation extends JavaInterface
{
    public void show();     
}

Now, I have created a class that implements the interfaces like below :

JavaInterface.java

public interface JavaInterface
{
    public void display();
}

TestInterfaceImplementation.java

public interface TestInterfaceImplementation
{
    public void show();     
}

Test.java

public class Test implements TestInterfaceImplementation, JavaInterface
{
   public void show()
    {
       System.out.println("\n show() method of TestInterfaceImplementation");
    }
   public void display()
    {
        System.out.println("\n display() method of JavaInterface");
    }
    public static void main(String args[])
     {
         Test test = new Test();
          test.show();
          test.display();
     }
}

Output :

When you will compile and execute the Test class then the output will be as follows :

Download Source Code

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: April 15, 2013

Related Tutorials

Ask Questions?    Discuss: Java Interface  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
DMCA.com