Is it possible in Java? Or can I use any method before define it?
I tried it in Abstract class but JVM said abstarct method can't be static. Then I tried define class to object but i did not use 'extends'. Both of them gave me a error.
Yes it's possible to call an abstract method before defining it in the abstract class. Assume you have designed a base formula class:
public abstract class AbstractFormula {
protected abstract void initialize();
public double evaluate() {
initialize();
//
// do the logic
//
return 0d; // the result
}
}
Also assume the logic for evaluating formula is to:
So the default implementation of the evaluate()
method is to call the initialize()
method and then do the calculation. In other word, you may not know the logic of initializing coefficient in all possible implementations of AbstractFormula
, but you can call it in other methods of the abstract class.
But you can NOT create a static abstract method in the first place. Because static methods are class methods and can be called without creating a object of that class. So having an static abstract method is pointless. static
methods should have their body when they are being defined.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments