Example: This example is covered when discussing abstract classes. The example is modified such that instead of declaring Shape class as an abstract class it is declared as an interface. We cannot have any implementation in the Shape interface. The interface declares two methods, which must be implemented by all sub-classes of this interface. This would duplicate the code of
method calCost() in all the implementing classes.
interface Shape
{
double puCost = 100;
double area();
double calCost();
}
class Triangle implements Shape
{
double s1,s2,s3;
Triangle(double a, double b, double c)
{
}
public double calCost()
{
}
public double area()
{
s1 = a; s2 = b; s3 = c;
return puCost * area();
double s = (s1+s2+s3)/2;
return Math.sqrt(s*(s-s1)*(s-s2)*(s-s3));
}
}
class Rectangle implements Shape
{ double s1,s2;
Rectangle(double a, double b)
{
}
public double calCost()
{
}
public double area()
{
}
}
class Square implements Shape
{ double s;
Square(double a)
{ s = a;
}
public double calCost()
{
}
public double area()
{ return s * s;
s1 = a; s2 = b;
return puCost * area();
return s1 * s2;
return puCost * area();
}
}
class ShapeTest2
{ static public void main(String args[])
{ Shape s1 = new Triangle(3,4,5);
Shape s2 = new Rectangle(3,4);
Shape s3 = new Square(3);
System.out.println(s1.area());
System.out.println(s2.area());
System.out.println(s3.area());
System.out.println(s1.calCost());
System.out.println(s2.calCost());
System.out.println(s3.calCost());
}
}
Output:
6.0
12.0
9.0
600.0
1200.0
900.0
Example: This example improves the previous one by introducing additional sub-class AbstractShape between interface Shape and its sub-classes. This sub-class can contain the generic code for method calCost().
interface Shape
{
double area();
double calCost();
}
abstract class AbstractShape implements Shape
{ double puCost;
AbstractShape(double puCost)
{ this.puCost = puCost;
}
public double calCost()
{ return puCost * area();
}
}
class Triangle extends AbstractShape
{ double s1,s2,s3;
Triangle(double a, double b, double c, double puCost)
{ super(puCost);
}
public double area()
{ double s = (s1+s2+s3)/2;
}
}
class Rectangle extends AbstractShape
{ double s1,s2;
Rectangle(double a, double b, double puCost)
{ super(puCost);
}
public double area()
{ return s1 * s2;
}
}
class Square extends AbstractShape
{ double s;
Square(double a, double puCost)
{ super(puCost);
}
public double area()
{ return s * s;
}
}
s1 = a; s2 = b; s3 = c;
return Math.sqrt(s*(s-s1)*(s-s2)*(s-s3));
s1 = a; s2 = b;
s = a;
class ShapeTest3
{ static public void main(String args[])
{ Shape s1 = new Triangle(3,4,5,10);
Shape s2 = new Rectangle(3,4,100);
Shape s3 = new Square(3,1000);
System.out.println(s1.area());
System.out.println(s2.area());
System.out.println(s3.area());
System.out.println(s1.calCost());
System.out.println(s2.calCost());
System.out.println(s3.calCost());
}
}
Output:
6.0
12.0
9.0
60.0
1200.0
9000.0
Example: The following example demonstrates that interface of the stack could be kept separate from its implementation by abstracting it using the interface StackInterface.
interface StackInterface
{ void push(int x);
int pop();
}
class Stack implements StackInterface
{ int top = -1; int a[] = new int[10];
public void push(int x)
{ if(top == 9)
{ System.out.println("Stack Full");
return;
}
top = top + 1; a[top] = x;
}
public int pop()
{ if(top == -1)
{ System.out.println("Stack Empty");
return -1; //assuming that -1 is not a valid data
}
int x = a[top]; top = top - 1; return x;
}
}
class StackTest
{ public static void main(String args[])
{ Stack st1=new Stack();
Stack st2=new Stack();
for(int i=1;i<5;i++)
{ st1.push(i);
}
for(int i=1;i<5;i++)
{ st2.push(i+10);
}
for(int i=1;i<5;i++)
{ System.out.println(st1.pop());
}
for(int i=1;i<5;i++)
{ System.out.println(st2.pop());
}
}
}
Output:
4
3
2
1
14
13
12
11
Example: This example demonstrates that an interface can be extended. This example also illustrates that a class implementing the extended interface must implement methods in base interface as well as methods in the extended interface.
interface ExtStackInterface extends StackInterface
{ public boolean isStackEmpty();
public boolean isStackFull();
public void display();
}
class ExtStack implements ExtStackInterface
{ int top = -1; int a[] = new int[10];
public void push(int x)
{ if(top == 9)
{ System.out.println("Stack Full"); return;
}
top = top + 1; a[top] = x;
}
public int pop()
{ if(top == -1)
{ System.out.println("Stack Empty");
return -1; //assuming that -1 is not a valid data
}
}
public void display()
{ for(int i = top; i >= 0; i--)
int x = a[top]; top = top - 1; return x;
{ System.out.println(a[i]);
}
}
public boolean isStackEmpty()
{ if(top == -1)
return(true);
else
return(false);
}
public boolean isStackFull()
{ if(top == 9)
return(true);
else
return(false);
}
}
Example: This example demonstrates that a class implementing the interface can also extend a base class. The implementing class must provide the implementation of all the methods in the interface. But the methods defined in the super-class need not be implemented as their definition is inherited from the super-class.
class ExtStack extends Stack implements ExtStackInterface
{ public void display()
{ for(int i = top; i >= 0; i--)
{ System.out.println(a[i]);
}
}
public boolean isStackEmpty()
{ if(top == -1)
return(true);
else
return(false);
}
public boolean isStackFull()
{ if(top == 9)
return(true);
else
return(false);
}
}
method calCost() in all the implementing classes.
interface Shape
{
double puCost = 100;
double area();
double calCost();
}
class Triangle implements Shape
{
double s1,s2,s3;
Triangle(double a, double b, double c)
{
}
public double calCost()
{
}
public double area()
{
s1 = a; s2 = b; s3 = c;
return puCost * area();
double s = (s1+s2+s3)/2;
return Math.sqrt(s*(s-s1)*(s-s2)*(s-s3));
}
}
class Rectangle implements Shape
{ double s1,s2;
Rectangle(double a, double b)
{
}
public double calCost()
{
}
public double area()
{
}
}
class Square implements Shape
{ double s;
Square(double a)
{ s = a;
}
public double calCost()
{
}
public double area()
{ return s * s;
s1 = a; s2 = b;
return puCost * area();
return s1 * s2;
return puCost * area();
}
}
class ShapeTest2
{ static public void main(String args[])
{ Shape s1 = new Triangle(3,4,5);
Shape s2 = new Rectangle(3,4);
Shape s3 = new Square(3);
System.out.println(s1.area());
System.out.println(s2.area());
System.out.println(s3.area());
System.out.println(s1.calCost());
System.out.println(s2.calCost());
System.out.println(s3.calCost());
}
}
Output:
6.0
12.0
9.0
600.0
1200.0
900.0
Example: This example improves the previous one by introducing additional sub-class AbstractShape between interface Shape and its sub-classes. This sub-class can contain the generic code for method calCost().
interface Shape
{
double area();
double calCost();
}
abstract class AbstractShape implements Shape
{ double puCost;
AbstractShape(double puCost)
{ this.puCost = puCost;
}
public double calCost()
{ return puCost * area();
}
}
class Triangle extends AbstractShape
{ double s1,s2,s3;
Triangle(double a, double b, double c, double puCost)
{ super(puCost);
}
public double area()
{ double s = (s1+s2+s3)/2;
}
}
class Rectangle extends AbstractShape
{ double s1,s2;
Rectangle(double a, double b, double puCost)
{ super(puCost);
}
public double area()
{ return s1 * s2;
}
}
class Square extends AbstractShape
{ double s;
Square(double a, double puCost)
{ super(puCost);
}
public double area()
{ return s * s;
}
}
s1 = a; s2 = b; s3 = c;
return Math.sqrt(s*(s-s1)*(s-s2)*(s-s3));
s1 = a; s2 = b;
s = a;
class ShapeTest3
{ static public void main(String args[])
{ Shape s1 = new Triangle(3,4,5,10);
Shape s2 = new Rectangle(3,4,100);
Shape s3 = new Square(3,1000);
System.out.println(s1.area());
System.out.println(s2.area());
System.out.println(s3.area());
System.out.println(s1.calCost());
System.out.println(s2.calCost());
System.out.println(s3.calCost());
}
}
Output:
6.0
12.0
9.0
60.0
1200.0
9000.0
Example: The following example demonstrates that interface of the stack could be kept separate from its implementation by abstracting it using the interface StackInterface.
interface StackInterface
{ void push(int x);
int pop();
}
class Stack implements StackInterface
{ int top = -1; int a[] = new int[10];
public void push(int x)
{ if(top == 9)
{ System.out.println("Stack Full");
return;
}
top = top + 1; a[top] = x;
}
public int pop()
{ if(top == -1)
{ System.out.println("Stack Empty");
return -1; //assuming that -1 is not a valid data
}
int x = a[top]; top = top - 1; return x;
}
}
class StackTest
{ public static void main(String args[])
{ Stack st1=new Stack();
Stack st2=new Stack();
for(int i=1;i<5;i++)
{ st1.push(i);
}
for(int i=1;i<5;i++)
{ st2.push(i+10);
}
for(int i=1;i<5;i++)
{ System.out.println(st1.pop());
}
for(int i=1;i<5;i++)
{ System.out.println(st2.pop());
}
}
}
Output:
4
3
2
1
14
13
12
11
Example: This example demonstrates that an interface can be extended. This example also illustrates that a class implementing the extended interface must implement methods in base interface as well as methods in the extended interface.
interface ExtStackInterface extends StackInterface
{ public boolean isStackEmpty();
public boolean isStackFull();
public void display();
}
class ExtStack implements ExtStackInterface
{ int top = -1; int a[] = new int[10];
public void push(int x)
{ if(top == 9)
{ System.out.println("Stack Full"); return;
}
top = top + 1; a[top] = x;
}
public int pop()
{ if(top == -1)
{ System.out.println("Stack Empty");
return -1; //assuming that -1 is not a valid data
}
}
public void display()
{ for(int i = top; i >= 0; i--)
int x = a[top]; top = top - 1; return x;
{ System.out.println(a[i]);
}
}
public boolean isStackEmpty()
{ if(top == -1)
return(true);
else
return(false);
}
public boolean isStackFull()
{ if(top == 9)
return(true);
else
return(false);
}
}
Example: This example demonstrates that a class implementing the interface can also extend a base class. The implementing class must provide the implementation of all the methods in the interface. But the methods defined in the super-class need not be implemented as their definition is inherited from the super-class.
class ExtStack extends Stack implements ExtStackInterface
{ public void display()
{ for(int i = top; i >= 0; i--)
{ System.out.println(a[i]);
}
}
public boolean isStackEmpty()
{ if(top == -1)
return(true);
else
return(false);
}
public boolean isStackFull()
{ if(top == 9)
return(true);
else
return(false);
}
}