superk
superk

Reputation:

Java interface and inheritance

If we have:

public interface Foo{}

public class Bar implements Foo{...}

Is there a difference between:

public class BarBar extends Bar implements Foo{..}

and

public class BarBar extends Bar{..}

I see a lot of code like this and it always confuses me. Does BarBar need to implement Foo? I mean since it extends Bar to begin with isn't that already there? I guess my question is, what purpose does implementing Foo in BarBar serve here?

Upvotes: 8

Views: 2989

Answers (5)

mP.
mP.

Reputation: 18266

It could be argued that the second "extends X implements Y" is verbose but the extra few chars are a good reminder. Take a look at ArrayList etc I'm pretty sure they use the long form - extends AbstrsvtList implements List.

In the end most developers are fast typers so the extra few chars cost practically no time to type. Why are a so many goals about typing less when it should be about clarity and eliminating ambiguity and not stating all the facts...

Upvotes: 2

Amr Gawish
Amr Gawish

Reputation: 2025

Well I agree with all the comments that has been said about this issue that its really unnecessary implementation although I might do that in case I want to override the behavior of barbar concerning foo implementation so I might do that to enforce overriding those methods and make the IDE auto implement it for me!!

But its the same thing as overriding those functionality in barbar

Upvotes: 0

Jataro
Jataro

Reputation: 2588

There is no reason to implement foo. Because bar is a foo barbar will be as well.

Upvotes: 0

Pavel Minaev
Pavel Minaev

Reputation: 101555

There is no difference. The extra implements there is harmless, but useless.

Upvotes: 3

ChssPly76
ChssPly76

Reputation: 100686

The main difference is 15 completely unnecessary characters :-)

When your parent class implements some interface, all interface methods are either implemented by it or are defined (explicitly or implicitly) as abstract. Either way, your class extending the parent class inherits all those methods and implicitly implements the original interface.

Upvotes: 14

Related Questions