Showing posts with label implement more than one interface. Show all posts
Showing posts with label implement more than one interface. Show all posts

Monday, 17 February 2014

Class can implement more than one interface

In Java, a class can implement more than one interface.

Syntax
    class ClassName implements Interface1, Interface2{

    }

Example
interface Area{
  double getArea(double r);
}
 

interface Perimeter{
  double getPerimeter(double r);
}

class MyCircle implements Area, Perimeter{
  final double PI=3.1428;
  
  public double getArea(double r){
    return (PI * r * r);
  }

  public double getPerimeter(double r){
    return (PI * 2 * r);
  }

  public static void main(String args[]){
    MyCircle circle1 = new MyCircle();
    System.out.println(circle1.getArea(10));
    System.out.println(circle1.getPerimeter(10));
  }
}

Output
314.28
62.855999999999995
Some points to Remember

1. If a class implementing multiple interfaces, and more than one interface has same variable definition, then using the variable directly causes ambiguity to the compiler
interface Perimeter{
  double PI=3.14;
  double getPerimeter(double r);
}

interface Area{
  double PI=3.1428;
  double getArea(double r);
}

class MyCircle implements Area, Perimeter{
  public double getArea(double r){
    return (PI * r * r);
  }

  public double getPerimeter(double r){
    return (PI * 2 * r);
  }

  public static void main(String args[]){
    MyCircle circle1 = new MyCircle();
    System.out.println(circle1.getArea(10));
    System.out.println(circle1.getPerimeter(10));
  }
}

When you tries to compile the above program, compiler throws the below error
MyCircle.java:3: error: reference to PI is ambiguous, both variable PI in Area and variable PI in Perimeter match
return (PI * r * r);
^

MyCircle.java:7: error: reference to PI is ambiguous, both variable PI in Area and variable PI in Perimeter match
return (PI * 2 * r);

To make the program run, be specific about the variable PI you are going to use.
class MyCircle implements Area, Perimeter{
  public double getArea(double r){
    return (Area.PI * r * r);
  }

  public double getPerimeter(double r){
    return (Perimeter.PI * 2 * r);
  }

  public static void main(String args[]){
    MyCircle circle1 = new MyCircle();
    System.out.println(circle1.getArea(10));
    System.out.println(circle1.getPerimeter(10));
  }
}

Output
314.28
62.800000000000004

2. Can interface be final?
No, Interfaces can't be final. A final class is not extendable. If you make interface as final, there is no use of it. So it is illegal in java.

final interface Area{
  double PI=3.1428;
  double getArea(double r);
}

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

Area.java:1: error: illegal combination of modifiers: interface and final
final interface Area{
^
1 error




Interface as reference types                                                 Extending interfaces                                                 Home

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