Jan 19 2012

Java stuff that you should know: difference between abstract classes and interfaces

Category: English posts,Miscellaneous,TechnicalIuliana @ 17:14

Many times after a Java Interview I have asked myself if my answers were correct, if the person interviewing me realized that I understand a certain concept and if I was able to express my knowledge in order to achieve that. So I will start writing on my blog how I would answer some common Java Interview questions.

So, in Java what are the differences between an abstract class and an interface?
This is a question with one big answer, not only because there are that many differences between abstract classes and interfaces, but because there are so many ways to say it. So, here is what I would say about interfaces:

  1. Interfaces are equivalent to protocols. They basically represent an agreed-upon behavior (a contract) to facilitate interaction between unrelated objects.
  2. Interfaces are a capsule for a group of methods with empty bodies or prototypes (abstract methods ) and implicitly final static members (constants).
  3. Interfaces have all members public impilicitly.
  4. Interfaces are declared like this:
    public interface TestIntf {}
  5. Interfaces can only extend other interfaces and keyword implements does not apply to them.
  6. Interfaces can extend more interfaces.
  7. If an interface has no direct superinterfaces, then the interface implicitly declares a public abstract member method m with signature s, return type r, and throws clause t corresponding to each public instance method m with signature s, return type r, and throws clause t declared in Object, unless a method with the same signature, same return type, and a compatible throws clause is explicitly declared by the interface.
  8. Interfaces are considered to be slower because they require extra indirection to find the corresponding method in the class implementing them.
  9. Interfaces support only public and default access modifiers.
  10. Interfaces can be declared abstract, so the Java compiler would not complain if your declaration would look like below, but the abstract modifier is redundant as interfaces are implicitly abstract.
    public abstract interface TestIntf {}

Here is what I would say about abstract classes:

  1. Abstract Classes are the middle ground between interfaces and classes. They can have both abstract and non-abstract methods and constant and non-constant fields.
  2. Abstract Classes can have constructor, but it can never be called directly. It can only be called using super in the extending class or is automatically called by the jvm.
  3. Abstract Classes support all access modifiers.
  4. Abstract classes can be declared like this:
    [access_modifier] abstract class TestAC {  [ implementation]    }
  5. Abstract Classes can implement any number of interfaces, can extend only one class (which can be abstract or not – the condition is not to be final) and implicitly extend class Object.
  6. Abstract Classes can have only non-abstract methods (but what would be the point?). Abstract Classes which have only abstract methods and constant fields are called fully abstract.

And what would I say about both of them:

  1. Java supports multiple inheritance only through interfaces.(Some say it is limited. I say it is practical.) Why? Because extending multiple classes may lead to problems when they have methods with identical signatures. (Remember C?)
  2. The use of interfaces or abstract classes is determined by the project requirements and design.
  3. Using abstract classes affects the flexibility of your code, because class extension rips away class individuality.

If you have anything else to add, or something here needs correction, your effort will be kindly appreciated.

Tags: , ,

4 Responses to “Java stuff that you should know: difference between abstract classes and interfaces

  1. bu7ch3r says:

    In java nu sunt metode virtuale pure? O clasa abstracta nu e o clasa care are cel putin o MVP de asta? Java nu zice: “cannot instantiate C.. because is abstract? OOP=istic vorbind fully abstract = = interface ?

  2. Rpx says:

    Nu amesteca Java cu C++… e ca și cum ai compara pisici cu câini… sunt mamifere, au 4 picioare, sunt drăguțe, dar funcționează și au roluri diferite.

  3. bu7ch3r says:

    :)) am incercat si eu :D Dupa ce am scris mi-am dat seama ca eu cant in alta gama :)

  4. Wild thoughts » Java stuff that you should know:What are the differences between C++ and Java? says:

    […] thoughts — seaqxx @ 16:48 Share Because somebody brought up this subject here I decided that this is the question I should answer next. I am a developer from the old […]

Leave a Reply