Introduction to Interfaces

Introduction to Interfaces


Posted in : Core Java Posted on : October 6, 2010 at 7:02 PM Comments : [ 0 ]

This section descibe about the interface in java.

Introduction to Interfaces

Interfaces are syntactically similar to classes but they don't have instance variables and methods are declared without any body.

Interface can be implemented by any number of classes. Also, any number of interface can be implemented by a single class. To implement an interface , a class must create the complete set of methods defined by the interface.

Defining a Interface

Interface can be defined as follows :

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;
}

Here-

access - access is either public or not used.

name - name is name of the interface used.

return-type - return type of the method.

type - type of the variable name.

final-varname - name of the variable of constant nature.

Parameter-list - List of the parameter of the method.

Note : Variable can be declared inside of  interface declarations. But they are implicitly final and static in nature means they can't be changed by the implementing class. They must also be initialized by constant value.

Example :

interface callback {
   int No= 0;
   int YES=1;
   int MAYBE=2
   int NEVER=3;
   void callback(int param);
}

Implementing interface

Same interface can be implemented by two or more classes. Interfaces can be implemented by classes using 'implements' keyword. The thing which should be keep in mind that when you implement an interface's method , it must be declared as public. Also, a class must create the complete set of methods defined by the interface.

Example :

Given below the interface :


public interface Callback {
	void callback(int param);
}

It is both permissible and common for classes that implement interface to define additional members of their own. Given below the Class which implements interface :


public class Client implements Callback {
	// implement callback's interface]
	public void callback(int p) {
		System.out.println("Callback called with " + p);
	}

	void OtherMethod() {
		System.out.println("Other member");
	}

}

Main Class :


public class InterfaceDemo {
	public static void main(String args[]) {
		Callback c = new Client();
		c.callback(42);
	}

}

Output :

C:\Program Files\Java\jdk1.6.0_18\bin>javac InterfaceDemo.java

C:\Program Files\Java\jdk1.6.0_18\bin>java InterfaceDemo
Callback called with 42

Interface can be extended

Using keyword 'extends', one interface can inherit another as :

interface A{
  void add1();
  void add2();
}

interface B extends A{
  void add3();
}

Point to remember : When a class implements an interface that inherits another interface , it must provide implementations for all methods defined within the interface inheritance chain.

For example :

class Myclass implements B{
    public void add1(){
       System.out.println("implement add1 method");
}
    public void add2(){
       System.out.println("implement add2 method");
}
    public void add3(){
       System.out.println("implement add3 method");
}

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics