Sunday 16 February 2014

Interfaces in Java

In JAVA, interfaces are reference types contain only constants, method signatures, nested types.

Interfaces doesn't contain method bodies, these are implemented by classes, extended by interfaces.

Defining an Interface
   interface InterfaceName{
      // constant declarations, if any
      // method signatures
      // Nested Types
   }

Note
All the methods in the interface are public.
All the variables in the interface are public static final.
Implementing An Interface
A class implements the interface provides the method body for the method signatures in the interface.

Syntax
    Class ClassName implements Interface1, Interface2, ...Interface N{
        //implements the interfaces
    }

Example
interface Circle{
  double PI = 3.1428;
  double getArea(int r);
}

 
class MyCircle implements Circle{
  public double getArea(int r){
    return (Circle.PI * r * r);
  }

  public static void main(String args[]){
    MyCircle circle1 = new MyCircle();
    System.out.println("Area of the circle is " +circle1.getArea(10));
  }
}


Output 
Area of the circle is 314.28
 
Some Points to Remember

1.If a class implements an interface, then it must implement all the methods in the interface, other wise, the class must declared as a abstract.
Example
interface Circle{
  double PI = 3.1428;
  double getArea(int r);
}

class MyCircle implements Circle{

}
   
When you tries to compile the class MyCircle, compiler throws the below error
MyCircle.java:1: error: MyCircle is not abstract and does not override abstract
method getArea(int) in Circle
class MyCircle implements Circle{
^
1 error

To make the program compile,there are two options
    1. Implement the methods signatures of the interface in the class.
   2. Make the class as abstract like below
        abstract class MyCircle implements Circle{

        }
  
2.By default all the methods in the interface are public, while implementing the interface, class must provide the public access specifier for the methods it implementing, otherwise “weaker access privileges” error thrown at compile time.

Example
interface Circle{ 
  double PI = 3.1428;
  double getArea(int r); 
}

class MyCircle implements Circle{
  double getArea(int r){
    return (Circle.PI * r * r);
  }
}

When you tries to compile the MyCircle class, compiler throws the below error.

MyCircle.java:2: error: getArea(int) in MyCircle cannot implement getArea(int) in Circle
double getArea(int r){
^
attempting to assign weaker access privileges; was public
1 error


3. All the variables in the interface are final by default, so updating the variable in a class causes the compile time error
Example
class MyCircle implements Circle{
  public double getArea(int r){ 
    Circle.PI=3.12; 
    return (Circle.PI * r * r);
  }
}

When you tries to compile the above program, compiler throws the below error
MyCircle.java:3: error: cannot assign a value to final variable PI
Circle.PI=3.12;
^
1 error


4. Can interface has static methods ?
Yes (From Java8 onwards)



Interfaces                                                 Interface as reference type                                                 Home

No comments:

Post a Comment