Monday, August 12, 2013

Programming in Java: Interfaces

Interfaces

Using the keyword interface, we can specify what a class must do, but not how it does it. Interfaces
are syntactically similar to classes, but they lack instance variables, and their methods are declared without any body. Once it is defined, any number of classes can implement an interface. Also, one class can implement any number of interfaces. So it is Java’s way of multiple inheritance.
To implement an interface, a class must create the complete set of methods defined by the interface. However, each class is free to determine the details of its own implementation.

Defining an Interface
An interface is defined much like a class. This is the general form of an interface:
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;
}
Notice that the methods that are declared have no bodies. They end with a semicolon after the parameter list. They are, essentially, abstract methods; there can be no default implementation of any method specified within an interface. Each class that includes an interface must implement all of the methods. Variables can be declared inside of interface declarations. They are implicitly final and static, meaning they cannot be changed by the implementing class.
Example
interface Area{
      double area();
      void displayArea();
}

Implementing Interfaces
Once an interface has been defined, one or more classes can implement that interface. To implement an interface, include the implements clause in a class definition, and then create the methods defined by the interface. The general form of a class that includes the implements clause looks like this:
class classname [extends superclass] [implements interface [,interface...]] {
// class-body
}
If a class implements more than one interface, the interfaces are separated with a comma.
Example
class Figure{
      int dim1, dim2;
      double ar;
}

class Circle extends Figure implements Area{
      public double area(){
                  return 3.14 * dim1 * dim1;
      }
      public void displayArea(){
                  System.out.println("Area of Circle: "+ar);
      }
      Circle(){
      }
      Circle(int r){
                  dim1 = r;
      }
}
class Rectangle extends Figure implements Area{
      public double area(){
                  return (double)dim1 * dim2;
      }
      public void displayArea(){
                  System.out.println("Area of Rectangle: "+ar);
      }
      Rectangle(){
      }
      Rectangle(int l, int b){
                  dim1 = l;
                  dim2 = b;
      }
}

public class IntTest{
      public static void main(String[] args){
                  Circle c1 = new Circle(10);
                  Rectangle r1 = new Rectangle(3,4);
                  c1.ar = c1.area();
                  r1.ar = r1.area();
                  c1.displayArea();
                  r1.displayArea();
      }
}
Here we have two classes Rectangle and Circle implementing the same interface Area. They also extends a class Figure. Both the classes provide implementations for the methods defined in Area, namely public double area()and public void displayArea(). This helps in bringing uniformity among all classes that implement Area.

Extending Interfaces
One interface can inherit another by use of the keyword extends. The syntax is the same as for inheriting classes. When a class implements an interface that inherits another interface, it must provide implementations for all methods defined within the interface inheritance chain.
Interface Interface1 extends Interface2{
      // body of Interface 1
}
Suppose a class implements Interface1, then it need to provide definition for all the methods in Interface1 as well as those in Interface2.

Interface Vs Abstract Class

Feature
Interface
Abstract class
Multiple inheritance
A class may inherit several interfaces.
A class may inherit only one abstract class.
Default implementation
An interface cannot provide any code, just the signature.
An abstract class can provide complete, default code and/or just the signature.
Access modifiers
An interface cannot have access modifiers for the functions, properties etc everything is assumed as public
An abstract class can contain access modifiers for the subs, functions, properties
Fields
If the interface contain any field, it is static and final by default
No such restriction
Instantiation
An Interface cannot be instantiated
An abstract class also cannot be instantiated

No comments:

Post a Comment