Incomplete Interface Illustration

  • 0
     Java provides classes a choice to to implement an interface completely or just partially. But, opting to implement an interface renders a class incomplete. It means that the class that implements an interface incompletely, must be declared Abstract. Any class that extends such class, must implement remaining methods of the interface or should declare themselves as Abstract too.
Fallowing program clearly shows the above stated concept. We have an interface named Figure which is to be placed in Figure.java.
package InterfaceIllustration;

interface Figure
{
 int area(int side1, int side2);
 int perimeter(int...side);
}
A class that implements this interface partially must be declared Abstract. Class Rectangle implements interface partially, so it is declared Abstract. Square class extends Rectangle, and is either supposed to implement the remaining methods of package Figure or to declared Abstract. This program is to be named as InCompleteTest.java
package InterfaceIllustration;

abstract class Rectangle implements Figure
{
 public int area(int length, int breadth)
 {
  return (length*breadth);
 }
}
class Square extends Rectangle
{
 public int perimeter(int...side)
 {
  int per = 0;
  for(int i:side)
   per = per + i;
  return per;
 }
}
class IncompleteTest
{
 public static void main(String...args)
 {
  Square sq = new Square();
  System.out.println("Area of the square with length 4 and breadth 4 is " + sq.area(4,4));
  System.out.println("Perimeter of square is " + sq.perimeter(4,4,4,4));
 }
}

Here is the sample output.
Area of the square with length 4 and breadth 4 is 16
Perimeter of square is 16

No comments:

Post a Comment