Skip to content

Instantly share code, notes, and snippets.

Created December 22, 2013 13:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/8082883 to your computer and use it in GitHub Desktop.
Save anonymous/8082883 to your computer and use it in GitHub Desktop.
Java Interface, Abstract Class and Concrete Class
public abstract class AbstractMathOperation implements MathOperation{
public void preOperate(Integer... values){
System.out.println("This is a pre operation ");
}
public void postOperate(Integer... values){
System.out.println("This is a post operation ");
}
public Integer operate(Integer... values){
preOperate(values);
Integer result = doOperation(values);
postOperate(values);
return result;
}
/**
* This method should be implemented by all the sub classes to implement actual logic "middle code"
*/
public abstract Integer doOperation(Integer... values);
}
public class Add extends AbstractMathOperation{
@Override
public Integer doOperation(Integer... values){
Integer i = 0;
for(Integer value : values){
i += value;
}
return i;
}
}
public class Main{
public static final MathOperation ADD = new Add();
public static final MathOperation SUBTRACT = new Subtract();
public static final MathOperation MULTIPLY = new Multiply();
public static final void main(String [] args){
System.out.ptintln(ADD.operate(1,2,3,4,5,6));
System.out.ptintln(SUBTRACT.operate(10,5,2));
System.out.ptintln(MULTIPLY.operate(1,2,3,4,5,6));
}
}
public interface MathOperation{
public Integer operate(Integer ... values);
}
public class Multiply extends AbstractMathOperation{
@Override
public Integer doOperation(Integer... values){
Integer i = 1;
for(Integer value : values){
i *= value;
}
return i;
}
}
public class Subtract extends AbstractMathOperation{
@Override
public Integer doOperation(Integer... values){
Integer i = 0;
for(Integer value : values){
i -= value;
}
return i;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment