Reputation: 43
I am to create an interface named Payable that has an abstract method named getPayableAmount that returns a double and takes no parameters, did I do this correctly? It seems far to simple to be right. Shouldn't there be a return statement or something? when i added one i got an error
public interface Payable
{
abstract double getPaymentAmount();
}
Upvotes: 0
Views: 1473
Reputation: 2099
JLS says this:
9.4 Abstract Method Declarations:
For compatibility with older versions of the Java platform, it is permitted but discouraged, as a matter of style, to redundantly specify the abstract modifier for methods declared in interfaces.
You also don't need to specify public
for interface methods.
See the documentation for Defining an Interface:
All abstract, default, and static methods in an interface are implicitly
public
, so you can omit the public modifier.
public interface Payable
{
double getPaymentAmount();
}
You'll need an implementation of the interface to actually implement the logic. For example something like this:
public class PayableImpl implements Payable
{
double getPaymentAmount()
{
// actual implementation that returns the payment amount
}
}
Upvotes: 2
Reputation: 1246
You don't need to provide the abstract keyword in an Interface method. All methods in an interface is an abstract method. No concrete methods are allowed in an interface.
An abstract class can have both abstract method and Concrete method. In that condition we need to specify if a method is an abstract one or a concrete one.
public interface Payable
{
double getPaymentAmount();
}
public abstract class Payable
{
//This is an abstract method. It has to be implemented by the extending class
abstract public double getPaymentAmount();
//This is a concrete method. It can be inherited by the extending class
private int CalucateSum(int a, int b)
{
return a+b;
}
}
Upvotes: 3