Dummy @ work

Rants and pains in software (with contributors)

Archive for May, 2010

Interface vs. abstract class

Posted by deroude on May 3, 2010

I had a casual phone interview last week, over a job I didn’t know I had applied for (thanks to my job search engines, probably).  The technical guy at the other end of the phone call asked me a few noob Java questions, to which I replied more or less amused, but definitely not seriously – due to my huge ego, the after-lunch full stomach mood and my epic resentfulness of phone conversations where something it as stake (more to the latter :P ). One of the questions was “what is the difference between a Java interface and an abstract class?” That got me.

Since then. I’ve been talking to people, trying to find an answer: why did the Java guys create interfaces? (If my phD professor sees this, he might squish me like the ignorant fly that I am, but I beseech him to first answer at length).

To put everyone’s mind at ease in my work department, I do know my Java. I know the grammar and semantics and the parts I don’t know (because I try hard to keep the details safely OUT of my mind) – I know quite well where to find. 

But in the end – what is the difference? In effect, why is the interface necessary?

  • Is it because one can extend only one abstract class (but implement more interfaces)? Okay, but why is that? An “extension” is a manifestation of an “is-a” relationship. So, why can a certain class “be-a” just one parent semantic?That is, if a “dog” “is-a” “mammal”, than how can I also make it a “pet” – since the classes are unrelated but seriously, they are not interfaces.
  • Interfaces do not implement methods and do not have a class “individuality” that can be grabbed when inherited. So, interfaces are sub-notions of abstract classes. Are they?
  • A very interesting phrase a friend of mine sent me via google: – “Interfaces can be implemented by classes that are not related to one another”. That is, as opposed to what? If there was an ontological relationship between direct extending classes of an abstract class, then the inheritance wouldn’t be direct any more. But I do admit some significance in this phrase – it probably relates to the “class facets” paradigm. You see interfaces as public utilities – so my question is: are they compiled differently from abstract classes? Do they simplify the resulting binary, make it faster?

Posted in Uncategorized | Leave a Comment »

 
Follow

Get every new post delivered to your Inbox.