Interface in Java :defining , implementing, extending interfaces, accessing implementations through interface references, nested interfaces

 Interface :

The interface is a blueprint that can be used to implement a class. It is similar to class. 
It is a collection of only abstract methods and static constants. 

Any service  requirement specification is called as interface.

Interface is an abstract type used to specify the behavior of a class.

-The interface does not contain any concrete methods (methods that have code).
-interface cannot contain instance fields(variables).
-interface cannot be instantiated.
-An interface cannot contains constructors or destructors.


Advantages of interfaces :


-Interfaces are used to achieve abstraction. 


-Interfaces are used to achieve multiple inheritance 


-Interfaces are used to achieve loose coupling.


Defining an Interface :


An interface is defined much like a class.
The interface keyword is used to declare an interface.


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


Example :

Here is a simple example to declare an interface. 


interface A
{
int a=10;
void show();


Compiler automatically converts methods of Interface as public and abstract, and the data members as public, static and final by default.

As compiler view of above example. 


interface A
{
public static final int a=10;
public abstract void show();
}


Implementing Interfaces :


one or more classes can implement that interface.
To implement an interface, include the implements keyword  in a class definition, and then create the methods defined by the interface.
The implementing class must provide body of all the methods in all the interfaces
otherwise it must be declared as abstract.


Syntax :
interface interface_name
{

}
class class _name implements interface_name
{
//Body of class
}
If a class implements more than one interface, the interfaces are separated with a comma.


Example :
interface A
{
int a=10;
void show();
}
class B implements A
{
   public void show()
   {
    System.out.println("implementation of                       interface") ;
  System.out.println("A value is:"+a);
  }
   public static void main(String args[])
   {
    B o=new B();
    o.show();
   }
}

OUTPUT :
implementation of interface
A value is :10

here, access modifier must be public in class B.


Extending Interfaces :


An interface can extend another interface in the same way that a class can extend another class. The extends keyword is used to extend an interface, and the child interface inherits the methods of the parent interface.


Syntax :
interface interface_name
{

}
interface interface _name extends interface_name
{

}


Example :
interface A
{  
void display(); 
 }  
interface B extends A
{
  void show();  
}  
class main implements B

 public void display()
{
System.out.println("Hello");
}
  public void show()
{
System.out.println("Welcome");
}    
public static void main(String args[])

 main  obj = new main();
  obj.display(); 
 obj.show();
   }
  }  


Output:
Hello
Welcome


Accessing Implementations Through Interface References :


You can declare variables as object references that use an interface rather than a class type.
Any instance of any class that implements the declared interface can be referred to by such
a variable. When you call a method through one of these references, the correct version
will be called based on the actual instance of the interface being referred to. This is one of
the key features of interfaces. The method to be executed is looked up dynamically at run
time, allowing classes to be created later than the code which calls methods on them.

 

Example :
interface shape
{
       double area();
}
class rectangle implements shape
{
  double l, b;
  {
     l=2.0; b=4.0;
   }
   public double area()
   {
      return l*b;
   }
}
class circle implements shape
{
      double r;
     {
      r=10.0;
     }
    public double area()
    {
      return 3.142*r*r;
     }
}
class square implements shape
{
  double s;
{
   s=3.0;
   }
   public double area()
   {
     return s*s;
    }
}
class compute
{
    public static void main(String args[ ])
   {
    shape s;
    rectangle r=new rectangle();
    circle c=new circle();
     square s1=new square() ;
     System.out.println("calculate area of              Geometrical figures");
      s=r;
     System.out.println("area of                                rectangle="+s. area()) ;
        s=s1;
      System.out.println("area of square="+

       s.area()) ;
       s=c;
       System.out.println("area of                                circle="+s.area()) ;

      }
}


Output :
calculate area of Geometrical figures
area of rectangle=8.0
area of square=9.0
area of circle=314.2


Interface Variables:


All variables in the interface are always public static and final because they are
common part for the access of the data types and methods for the implementing.

Example :
public static final int a=10;


Nested or Inner interfaces in Java.


An interface which is declared inside another interface or class is called nested interface. They are also known as inner interface.
We can declare interfaces as member of a class or another interface. Such an interface is called as member interface or nested interface.
The nested interface must be referred by the outer interface or class. It can't be accessed directly.


1.for nested interface :We can declare interfaces as member of another interface


Syntax :
interface interface_name
{   ... 
    interface nested_interface_name
     {    ...   
      }  
}   


Example :
interface A
{   
      interface B
      {     
        void display();    
        }
  }  
class main implements A.B

  public void display()
    {
System.out.println("Hello nested interface");
}    
 public static void main(String args[]){    
A.B o=new main();
    o.display();  
  }
  }  


Output :
hello nested interface


2.for member interface :We can declare interfaces as member of a class.

Syntax :
class class_name
{   ... 
    interface nested_interface_name
     {    ...   
      }  
}   


Example:
 class A
{   
      interface B
      {     
        void display();    
        }
  }  
class main implements A.B

  public void display()
    {
System.out.println("Hello nested interface");
}    
 public static void main(String args[]){    
A.B o=new main();
    o.display();  
  }
  }  


Output:
hello nested interface


Multiple inheritance in Java by interface.


Java doesn’t allow multiple inheritance to avoid the ambiguity caused by it.
But we can achieve multiple inheritance through interface.
Multiple inheritance by interface occurs if a class implements multiple interfaces or also if an interface itself extends multiple interfaces..
If a class implements multiple interfaces, or an interface extends multiple interfaces, it is known as multiple inheritance.


1.class can implement multiple interfaces.


Syntax :

interface A
{
// members of A
}
interface B
{
// members of B
}
class C implements A, B
{
// abstract members of A
// abstract members of B
}


Example :
interface A
{  
void display(); 
 }  
interface B
{
  void show();  
}  
class main implements A,B

 public void display()
{
System.out.println("Hello");
}
  public void show()
{
System.out.println("Welcome");
}    
public static void main(String args[])

 main  obj = new main();
  obj.display(); 
 obj.show();
   }
  } 


Output:
Hello
Welcome


2.interface can extend multiple interfaces.


Syntax :
interface A
{ ... }
interface B
{ ... }
interface C extends A, B
{ ... }


Example :
interface A
{
void display();
}
interface B
{
void show();
}
interface C extends A, B
{
void print();
}
class main implements C
{
public void display()
{
System.out.println("Hello");}
public void show()
{
System.out.println("Welcome");
}
public void print()
{
System.out.println("Hi");
}
public static void main(String args[]){
main obj=new main();
obj.display();
obj.show();
obj.print();
}
}


OUTPUT :
Hello
Welcome
Hi


What is marker or tagged interface?


An interface which has no member is known as a marker or tagged interface.


Example 1:
public interface Serializable
{  
}  
Example 2:
public interface Remote
{  
}  


Partial Implementations :


If a class includes an interface but does not fully implement the methods defined by that
interface, then that class must be declared as abstract. 

For example :

interface A

{

       void display(); 

         void show();

abstract class Incomplete implements A

 {
int a, b;
 public void show() {
System.out.println(a + " " + b);
}
// ...
}
Here, the class Incomplete does not implement display ( ) and must be declared as abstract.
Any class that inherits Incomplete must implement display( ) or be declared abstract itself.


Can we define a class inside the interface?


Yes. 


Example :


interface A
{    

class B

{

 } 
 }  

Comments

Popular posts from this blog

Control Statements:Selection statement ,Iteration statement and Jump statement in Java

Applets - Inheritance hierarchy for applets, differences between applets and applications, life cycle of an applet, passing parameters to applets, applet security issues.