INTERFACE

(Written by- Krishnamoorthy D)

(Emailid: [email protected])

(Designation: SMTS in Oracle India Pvt Ltd., Bangalore.)

Google

 

This tutorial gives the more detail about the interface and possible ways to use.

 

Introduction:    

OOPs: Interface is the way to say external behavior to the particular object. This behavior may be used for other Object, which is created from some other class.

 

Interface is a purely abstract class. Interface technique is found to implement the encapsulation. In Java, multiple inheritances are not possible. Interface gives the facility to implement this multiple inheritance.

 

In modeling language, implementing Interface association is called as realization.

 

            The variables, which are declared in interface, are public, final, and static.

 

            Class can inherit only one other class, but one or more interface can be implemented for a class.  This class should implement all the methods, which are declared in interfaces. If the class is abstract then there is no need to implement, but the class should implement which is inheriting this abstract class.

Simple Interface:

            Interface can contain methods and variables. Variables should be initialized.

Syntax:

interface <interface name>{

<List of variables with initialization value ;>

<List of method definition ;>

}

Example:

interface  I1{

 String city="CBE";

 String getCity();

}

Class within Interface:

Interface can contain class also. This class has public visibility to the class, which implements this interface. Compiler for the class within the Interface implicitly assumes public access modifier. Static class also is in interface.

 

interface I {

 class G{//may static

 int I;

 }

}

 

class C implements I{

G interfaceClass=new G();

}

Nested Interface:

            Interface may contain another interface. This interface will be called using the parentinterface.chldinterface from outside the parent interface. If the interface implemented within the same interface, then parent interface name is not required to implement child interface.

 

interface parent

{

  interface child{ void sayHallo();}

  class  English implements child { 

  public  void sayHallo() {System.out.println("Hello");}

  }

}

class German implements parent.child{ 

public void sayHallo(){System.out.println("Hallo");}

}

 

 

Access modifier:

            Interface can be declared with the access modifier. If the interface is declared not within the class or interface, it can't be declared as private interface. It should be some other access modifier.

Abstract Keyword:

            Interface can be declared with the keyword "abstract". This keyword does not give any additional interface to compiler. If this keyword is not given to interface, compiler will assumes itself.

Interface within Class:

Interface may be declared within the class, and Interface. Interface can't be declared within the method.

 

            If the interface is declared within the class, this interface will be implemented by some other class which are declared within same class or can be accessed through the other class. Interface declared within Class will be treated as static implicitly.

 

class C{

 

interface I{int getI();} //we can type as  static interface I{int getI();}

 

class SubClass implements I{

int getI(){return 1;}

}

 public static void main(String[] args) {

 I i=new SubClass(); 

 }

 

}

 

class SometherClass implements C.I{

public static void main(String[] args) {

C.I i=new SometherClass();

}

}

 

Interface extends Interface:

            Inheritance is possible in interface also. An interface can extend one or more interfaces using the keyword. Interface can't implement other interface.

 

interface I1{}

interface I2{}

interface I3 extends I1,I2{} // see more detail in Overriding Method

 

 

Variables:

          All the variables what are declared in interface are public, final, and static. The value should be assigned in interface itself. This value can't be initialized in constructor method like normal final variable in class.

 

Access Modifier/Access specifier:

Access modifier and Access specifier is not required to explicit to interface variables.

 

Ambiguous field:

            Two interfaces has same variable and both the interface are implemented in a class then we will get Ambiguous error.

 

public interface I1 { public int i=0; }

 

public interface I2 { public int i=10; }

 

public class C implements I1,I2{       

    public static void main(String[] args) {

    int k= i; // The field i is Ambiguous

    }

}

 

 

Overriding Field:

            An Interface has one variable. Another one interface extends this interface and declares the same variable, this is called overriding field in interface.

 

public interface I1 { public int i=0; }

 

public interface I2 extends  I1{

public int i=0; //overridden variable

}

 

Methods:

Methods, which are defined in the interface, are abstract method and also public one. This method should be implemented in the class, which is implementing this interface.

            More than one interface can have same signature method and may these interfaces implemented in a single class. It won't give any ambiguous error. This is the good future in java.

Interfaces implemented class's object can be assigned to interfaces. This object is called as Interface object and this will give the access to the method what are all defined with same signature within this interface.

 

public interface I1 {

   public int i=0;

   public int getI();

}

 

public interface I2 {

  int getI();

}

 

public class C implements I1, I2 {

  public static void main(String[] args) {

    C c=new C();

    I1 i1=c;// getI() method is from I1

    I2 i2=c; // getI() method is from I2

   }

   public int getI() {

    return i;

   }

}

 

In the above code, int getI() are the methods in both the interfaces. They were defined differently but both are same. getI() method will perform the same operation when this called from any of this two interface objects.

 

 

 

Overriding Method:

            An Interface has one method. Another one interface extends this interface and defines method with the same signature, this is called overriding method in interface. In interface, Overriding is not really happened.

 

public interface I1{ 

public int getI();

}

public interface I2 extends  I1{

  int getI(); // overridden method

}

public class C implements  I2 {

  public static void main(String[] args) {

    C c=new C();

    I1 i1=c;// getI() method is from I1

    I2 i2=c; // getI() method is from I2

   }

   public int getI() {    return i;   }}

 

Now the object c is assigned to the super interface I1 of I2.  I1 and I2 have same method. When ever object is assigned to I1, implemented getI() method will be mean as from I1.

 

Implement Interface in Abstract Class:

            Interface may be implemented in Abstract class. There is no need to implement all the methods. These methods may be implemented later in the class which is extending this abstract class.

            Instead of implementing the method, can be declared as abstract method in abstract class.

 

interface I

{

void m1();

void m2();

}

 

abstract class C implements I

{

public abstract void m1(); // m1 method is declared as abstract

// m2 is not implemented

}

 

 

 

Hosted by www.Geocities.ws

1