Pranav Shah
Pranav Shah

Reputation: 3323

Is interface required on a class extending an abstract class already implementing the interface?

Assume there is a code as such:

package com.ps.Sample;

public interface Sample
{
    public void Method1();
}

public abstract class AbstractSample implements Sample
{
    public void Method1()
    {
        System.out.println("Hello World");
    }
}

public class MySample extends AbstractSample
{

}

public class TestSample
{
    public static void main(String[] args) 
    {
        Sample my = new MySample();

        my.Method1();
    }

}

My question is: Is there any benefit to declaring the concrete class as

public class MySample extends AbstractSample implements Sample

instead of

public class MySample extends AbstractSample 

Upvotes: 2

Views: 151

Answers (2)

pauljwilliams
pauljwilliams

Reputation: 19225

One benefit would be that if AbstractSample was changed to not implement Sample, the first declaration would still allow you to pass instances of MySample to methods expecting a Sample.

Upvotes: 4

JB Nizet
JB Nizet

Reputation: 691645

No, there is not. It's redundant. AbstractSample is a Sample, and MySample is a AbstractSample. So MySample is a Sample.

The javadoc displays all the implemented interfaces anyway, whether you add the implements Sample or not.

Upvotes: 9

Related Questions