I am wondering how to implement an abstract method in an interface. Given this interface:
public interface Analysis
{
public double getAverage();
}
To implement the abstract method, do I just declare the method as abstract?
public abstract double getAverage(); ??
or declare the interface as abstract?
public abstract interface Analysis
{
public double getAverage();
}
Or are other things needed?
I have a class that implements the interface called Marks
:
public class Marks implements Analyzable
{ some other code
constructor
开发者_JAVA技巧 mutators
accessor
public double getAverage();
}
So, my main problem is How to implement the abstract method in interface?
To make an abstract class implementing the interface:
public abstract class AbstractAnalysis implements Analysis {
public abstract double getAverage();
}
To make a concrete class implementing the interface:
public class MyAnalysis implements Analysis {
public double getAverage() {
// TODO
}
}
Or, to make a concrete class extending the abstract class and thereby implementing the interface:
public class MyAnalysis extends AbstractAnalysis {
public double getAverage() {
// TODO
}
}
If you simply declare your abstract class to implement Analysis
, without implementing getAverage()
or declaring it in your abstract class, concrete subclasses of your abstract class will be required to provide an implementation of that method.
Every method in an interface will be abstract. This is one of the specifications in Java.
http://java.sun.com/docs/books/jls/second_edition/html/interfaces.doc.html
精彩评论