Java interface complete tutorial

An interface in the Java programming language is an abstract type that is used to specify an interface (in the generic sense of the term) that classes must implement. Interfaces are declared using the interface keyword, and may only contain method signature and constant declarations (variable declarations that are declared to be both static and final). An interface may never contain method definitions.

Interfaces cannot be instantiated, but rather are implemented. A class that implements an interface must implement all of the methods described in the interface, or be an abstract class. Object references in Java may be specified to be of an interface type; in which case, they must either be null, or be bound to an object that implements the interface.

One benefit of using interfaces is that they simulate multiple inheritance. All classes in Java must have exactly one base class, the only exception being java.lang.Object (the root class of the Java type system); multiple inheritance of classes is not allowed.

interface are designed to supperot dynamic method resolution at run time .Normally  in order for a method to be called from one class to another ,both classes need to be present at compile time so the java compiler can check to ensure  that the method signatures are compitable .This requirement by itself makes for a static and nonextensible classing environement .Inevitably in a system like this functionality gets pushed up higher and higher in the class  hierarchy so that the mechanisam will be available to more and more subclasses .Interfaces are designed to avoid this problem .They disconnect the definition of a method or set of methods from the inheritance hierachy .Since interfaces are in a different hierarchy from classes .It is possible for classes that are unrelated in terms of  the class hierachy to implement the same interface .This is where the real power of the interfaces is realized


Defining an interface



[visibility] interface InterfaceName [extends other interfaces] 
{
        constant declarations
        abstract method declarations
}

The body of the interface contains abstract methods, but since all methods in an interface are, by definition, abstract, the abstract keyword is not required. Since the interface specifies a set of exposed behaviors, all methods are implicitly public.


Thus, a simple interface may be


public interface Area
  {
     
       void areacalc(int p);
  }


The member type declarations in an interface are implicitly static, final and public, but otherwise they can be any type of class or interface.


The syntax for implementing an interface uses this formula:


implements InterfaceName[, another interface, another, ...]



Classes may implement an interface. For example,



public class cal  implements Area 
{

        public void Area (int p)
         {
               // programming to eat prey p (specifically for a lion)
         }
}


If a class implements an interface and does not implement all its methods, it must be marked as abstract. If a class is abstract, one of its subclasses is expected to implement its unimplemented methods. Although if any of the abstract class' subclasses does not implement all interface methods, the subclass itself must be marked again as abstract.


Classes can implement multiple interfaces



public class calc  implements Area,...........



Interfaces are commonly used in the Java language for callbacks. Java does not allow the passing of methods (procedures) as arguments. Therefore, the practice is to define an interface and use it as the argument and use the method signature knowing that the signature will be later implemented.


Sample Program for finding area of a circle


Program: area.java

interface shape
 {
   
     public void area_cal(int r);
    
    
 }
class circle implements shape
 { 

    public void area_cal(int r)
    {
       double a=3.14*r*r;
       System.out.println("The circle area is "+a);

    }
    
    
 }
public class area
{
         
    public static void main(String[] args)
    {
       
          shape circleshape=new circle();
         
             circleshape.area_cal(10);
    }
}
Output 



C:\java>javac area.java
C:\java>java area
The circle area is 314.0


Java interface complete tutorial


0 comments:

Post a Comment

 

learn java programming Copyright © 2011-2012 | Powered by appsackel.org