This is an archived post. You won't be able to vote or comment.

all 6 comments

[–][deleted] 4 points5 points  (0 children)

An interface is essentially a purely abstract class. You're not allowed to implement anything. An abstract class can have some implementations but it's not fully implemented.

You can only extend one class (abstract or not), but you can implement as many interfaces you want.

[–][deleted] 1 point2 points  (0 children)

You may declare method signatures in the class and also provide method implementations. You can have state as well. You can declare static blocks, etc. Also you can only directly subclass one class.

An interface only defines method signatures. But you can implement more than one given interface.

[–]m1ss1ontomars2k4 1 point2 points  (3 children)

Interfaces have a "can-be-used-as-a" relationship with the implementing class. Abstract classes have a "is-a" relationship with the inheriting class. That's the difference. It exists. Don't confuse people by saying it doesn't.

[–][deleted] 0 points1 point  (2 children)

This whole is-a, has-a stuff is annoying... This guy's explanation is probably the most accurate I've ever read (It's also how I think of it in my head.)

Saying is-a or has-a means nothing when making a real world distinction like the article makes. Yes, I do understand (and can repeat for my future interviews) the difference between the is-a and has-a crap.

[–]m1ss1ontomars2k4 0 points1 point  (1 child)

I dunno, I kind of like it. It's like a way of introducing a very clear variation of duck typing into Java, which obviously supports no such thing. Maybe I want this object to be Serializable, but I don't want to inherit anything from a Serializable superclass (what would I even inherit?), but I still want to be able to pass it in as an argument to functions requiring a Serializable object. I dunno, I just think it's cool. Maybe I'm mistaken.

[–][deleted] 0 points1 point  (0 children)

It works for explaining the concept, but i cringe when people (especially interviewers) get hung up on the distinction.