-
Interface Implementation
I am having a hard time trying to understand interface, how it works and why it is considered more efficient. Also, I have to implement an interface but the only thing I was able to do implement sadly was the printName method, and I am not sure it is correct, here is the code:
[code]
interface ArithmeticOperator {
// Returns the result of applying the operator to operands a and b.
double operate (double a, double b);
// Return a String that is the name of this operator.
public void printName (String a);//{
//String printName ();
};
class OperatorFactory implements ArithmeticOperator{
public void printName (String a){
System.out.println(a);
}
interface OperatorIterator {
// Apply the operator op repeatedly to the startValue, for numIterations
// times, e.g., for numIterations=4 you would return
// op (startValue, op (startValue, op (startValue, startValue)))
double iterate (ArithmeticOperator op, double startValue, int numIterations);
}
class OpIterator implements OperatorIterator{
public double iterate(ArithmeticOperator op, double startValue, int numIterations){
}
}
public class Exam1 {
public static void main (String[] argv)
{
// Test 1:
System.out.println ("TEST 1:");
ArithmeticOperator add = OperatorFactory.get ("add");
System.out.println (" 1 " + add.printName() + " 2 = " + add.operate (1,2));
ArithmeticOperator sub = OperatorFactory.get ("sub");
System.out.println (" 3 " + sub.printName() + " 2 = " + sub.operate (3,2));
ArithmeticOperator mult = OperatorFactory.get ("mult");
System.out.println (" 3 " + mult.printName() + " 2 = " + mult.operate (3,2));
ArithmeticOperator div = OperatorFactory.get ("div");
System.out.println (" 3 " + div.printName() + " 2 = " + div.operate (3,2));
// Test 2:
System.out.println ("TEST 2:");
ArithmeticOperator add2 = OperatorFactory.get ("add");
ArithmeticOperator sub2 = OperatorFactory.get ("sub");
System.out.println (" Does add2==add? " + add2.equals(add));
System.out.println (" Does add2==sub2? " + add2.equals(sub2));
// Test 3:
System.out.println ("TEST 3:");
OperatorIterator opIter = new OpIterator ();
System.out.println (" 3 * 8 = " + opIter.iterate(add, 3, 8));
System.out.println (" 3 ^ 4 = " + opIter.iterate(mult, 3, 4));
}
}
-
an interface describes how your class should look.
interface ArithmeticOperator {
// Returns the result of applying the operator to operands a and b.
double operate (double a, double b);
// Return a String that is the name of this operator.
public void printName (String a);//{
//String printName ();
};
this describes that if your class implements it there has to be a double operate
a public void printName(String a)
but this class
class OperatorFactory implements ArithmeticOperator{
public void printName (String a){
System.out.println(a);
}
hasn't got those methods.
It's just like you have an interface human.
a human has to have hair, legs, brains,...
but then there is a person jan created which implements human
one of his things are, he has hair but brown hair.
lets say human contains something like this:
public String getHairColor();
jan implements it so he has to have a method getHairColor and it returns in his cas "brown"
the advantage of this is:
you can make a new human: Human janperson=new jan();
janperson.getHairColor();
-
new human: Human janperson=new jan();
janperson.getHairColor(); same be example of polymorphism not example of interface
assume there has a superclass, shape
circule, rectangle etc extends shape
we may want to find out degree of inner Angles of shape(rectangle, triangle etc) by method getInnerAngle
but for circule, thats no such of inner angles.
getInnerAngle is not good to be a method of shape, as it is no use for some of subclass (circle)
than interface will be the choose. only subclasses will inner Angles implements a interface with getInnerAngle method
-
damn, found it always hard to understand polymorphism, looks like I am a bit confused :)