ViniciusArruda
ViniciusArruda

Reputation: 990

Multiple inheritance or Multiple interfaces

I'm new in Java and I would like to know what solution is more correct and why.

I have these two interfaces:

interface Account
{
    public double getBalance();
}

interface Taxable
{
    public double calculateTaxes();
}

And I need a class that implements these two interfaces.

For me, is more intuitive this solution:

class TaxableAccount implements Account, Taxable
{
    @Override
    public double calculateTaxes()
    {
        return 10;  //just for example
    }

    @Override
    public double getBalance()
    {
        return 20;
    }

}

But I read a book that has this solution:

interface TaxableAccount extends Account, Taxable
{
}

class AccountWithTaxes implements TaxableAccount
{
    @Override
    public double calculateTaxes()
    {
        return 10;
    }

    @Override
    public double getBalance()
    {
        return 20;
    }
}

Thanks.

Upvotes: 2

Views: 101

Answers (1)

Software Engineer
Software Engineer

Reputation: 16110

The TaxableAccount interface would be pointless unless you intend to add something to it -- say at least one extra method. An empty interface is a 'code smell' and should be removed, leaving you with the perfectly fine first example.

Another clue that this is a 'smell' is that you've had trouble coming up with a good, simple name for the class in the second example - mostly, naming should be obvious and self-evident, as it is in the first example.

Upvotes: 3

Related Questions