This discussion is archived
1 2 3 Previous Next 34 Replies Latest reply: Nov 18, 2006 5:05 AM by 807599 RSS

what is the difference between an interface and an abstract class?

807597 Newbie
Currently Being Moderated
so whats the difference ??!! i know i may sound stupid but im kindda a beginner in java technology and this question is killing me ....so please can someone explain it to me in simple words !!! ....what i know is that you IMPLEMENT an interface class (in which you can implement many interface classes) while you can only EXTEND one abstract class (i.e JFrame) ...is this true ??!
  • 1. Re: what is the difference between an interface and an abstract class?
    807597 Newbie
    Currently Being Moderated
    An interface has all methods abstract. An abstract class can have zero or all methods abstract :)
  • 2. Re: what is the difference between an interface and an abstract class?
    dcminter Newbie
    Currently Being Moderated
    An interface is a contract. It says: "I hereby promise that my class will have the following methods, and if it doesn't you can crash with impunity".

    An abstract class is similar, but it also (usually) says "...and also it has these methods implemented like this."

    There is a dramatic pratical difference - multiple interfaces can be implemented, but you can only extend one class (abstract or otherwise). So generally if you get a choice, you want to implement an interface - only when your class shares substantial functionality with another class do you want to extend it, and even then you might prefer to implement an interface anyway.

    A good example of an interface is Runnable. It promises that classes extending it will have a method "public void run()".

    A good example of a class is AbstractList. It provides most of the things that you want to use, in order to imeplement a list, saving you the trouble - but if you prefer not to, you can just implement the List interface.

    Dave.
  • 3. Re: what is the difference between an interface and an abstract class?
    807597 Newbie
    Currently Being Moderated
    thanks everyone for quick replies :) ...i really love this place ... but theres another stupid question ,is every class an abstract class ??! i cant seem to understand the word (abstract) ,far as i know that every abstract mthod must be implemented ...im kindda weak in the object oreinted thing and im embarassed to ask any of my friends these questions ...thank you for your help people ...and one more thing ,if i impelement an interface or class ,should i use all the inherited methods ??! am i forced to use them or over ride them ??! and the same thing goes for EXTENDS ,,,whats the difference between them ??!
  • 4. Re: what is the difference between an interface and an abstract class?
    807597 Newbie
    Currently Being Moderated
    Abstract class is just a class which cannot be instantiated. A class is declared abstract to indicate that some method does not have an implementation, and instantiating such a class would be crazy!
  • 5. Re: what is the difference between an interface and an abstract class?
    807597 Newbie
    Currently Being Moderated
    Thanks annie79 ,you've been really helpful :)
  • 6. Re: what is the difference between an interface and an abstract class?
    dcminter Newbie
    Currently Being Moderated
    Every class can be extended. "Abstract" just means "Not Finished" for our purposes.

    The idea is that you can come up with an abstract notion of, for example, a Doofus.

    Our Abstract Doofus looks like this:
    abstract public Doofus {
       public Doofus(int iq) {
          this.iq = iq;
       }
       public int getIq() {
          return this.iq;
       }
    
       // Not finished - this is an ABSTRACT doofus, not a 
       // CONCRETE doofus.
       public abstract void saySomethingDumb();
    
       private int iq;
    }
    Because our Doofus isn't finished, you can't actually create one. Hence it's an abstract representation of Doofusness, not an actual concrete example of a Doofus.

    But this one, you can:
    public class EnglishDoofus extends Doofus {
       public EnglishDoofus(int iq) {
          super(iq);
       }
    
       public void saySomethingDumb() {
          System.out.println("I think spam is underrated");
       }
    }
    That one you can create, because it's finished. We know we can call the getIq method on it, but we got that for free because we used the existing behaviour of the super class.

    Here's another Concrete example of a Doofus:
    public class FrenchDoofus extends Doofus {
       public FrenchDoofus(int iq) {
          super(iq);
       }
    
       public void saySomethingDumb() {
          System.out.println("Quoi?");
       }
    }
    Again, it's a REAL Doofus, not an abstract one. Again, we've saved a bit of work - but we're also now able to take advantage of polymorphism, because without knowing which particular Doofus we've got, we can still make it say something dumb:
    Doofus[] idiots = new Doofus[] { new EnglishDoofus(42), new FrenchDoofus(69) };
    for(int i = 0; i < idiots.length; i++ ) {
       System.out.println("The IQ of the current Doofus is: " + idiots.getIq());
    System.out.println(idiots[i].saySomethingDumb());
    System.out.println();
    }

    Does that clarify things a little?

    Dave.
  • 7. Re: what is the difference between an interface and an abstract class?
    800322 Newbie
    Currently Being Moderated
    and one more thing ,if i
    impelement an interface or class ,should i use all
    the inherited methods ??! am i forced to use them or
    over ride them ??!
    You're forced to implement all abstract methods (those of an interface are implicitely abstract) you get by inheritance (abstract class) or extension (interface), unless you declare your class to be abstract, too. Obviously. A concrete class has to be able to provide all offered methods...

    Ideally, you neither need to use any inherited method (only do so if it makes sense - nobody can force you to call a method), nor to override them. By extending a superclass, you're basically adding special functionality that wasn't there, meaning: you can do everything the superclass can, and then some more. If you need to override a lot, it's rather a sign that you shouldn't inherit at all, because your class seems to be doing something completely different from what the superclass did. Keep in ming: inheritance is only appropriate for "is a special kind of" relationships.
    and the same thing goes for
    EXTENDS ,,,whats the difference between them ??!
    Extends is used for inheritance from classes, including all of the superclass's method implementations and member variables. Implements just says: "have a look at that interface - this class provides all of those methods".
  • 8. Re: what is the difference between an interface and an abstract class?
    807597 Newbie
    Currently Being Moderated
    thanks everyone and especially to dcminter ,your example has been very helpful :)
  • 9. Re: what is the difference between an interface and an abstract class?
    807597 Newbie
    Currently Being Moderated
    To help explain, first let me introduce some terminology that I hope will help clarify the situation. I will say that a "fully" abstract class is an abstract class in which ALL methods are abstract. In contrast, a "partially" abstract class is an abstract class in which some of the methods are abstract, and some are concrete (i.e. have implementations).

    Q: OK. So what is the difference between a fully abstract class and an interface?

    A: Basically, none. They are the same.

    Q: Then why does Java have the concept of an interface, as well as the concept of an abstract class?

    A: Because Java doesn't support multiple inheritance.

    Q: Huh??!!!

    A: Java has a rule that a class can extend only one abstract class, but can implement multiple interfaces (fully abstract classes).

    There's a reason why Java has such a rule.

    Remember that a class can be an abstract class without being a FULLY abstract class. It can be a PARTIALLY abstract class.

    Now imagine that that we have two partially abstract classes A and B. Both have some abstract methods, and both contain a non-abstract method called foo().

    And imagine that Java allows a class to extend more than one abstract class, so we can write a class C that extends both A and B. And imagine that C doesn't implement foo().

    So now there is a problem. Suppose we create an instance of C and invoke its foo() method. Which foo() should Java invoke? A.foo() or B.foo()?

    Some languages allow multiple inheritance, and have a way to answer that question. Python for example has a "method resolution order" algorithm that determines the order in which superclasses are searched, looking for an implementation of foo().

    But the designers of Java made a different choice. They choose to make it a rule that a class can inherit from as many FULLY abstract classes it wants, but can inherit from only one PARTIALLY abstract class. That way, the question of which foo() to use will never come up.

    This is a form of limited multiple inheritance. Basically, the rule says that you can inherit from (extend) as many classes as you want, but if you do, only one of those classes can contain concrete (implemented) methods.

    So now we do a little terminology substitution:

    "abstract class" = "class that contains at least one abstract method, and can also contain concrete (implemented) methods"

    "interface" = "class that is fully abstract -- that cannot contain any concrete methods"

    And you get the familiar Java rule: A class can extend at most one abstract class, but may implement many interfaces.
  • 10. Re: what is the difference between an interface and an abstract class?
    807597 Newbie
    Currently Being Moderated
    Stephen_Ferg,

    That's the best explanation I've read on here to this often asked question. Thanks.
  • 11. Re: what is the difference between an interface and an abstract class?
    807597 Newbie
    Currently Being Moderated
    "abstract class" = "class that contains at least one
    abstract method, and can also contain concrete
    (implemented) methods"
    If one or more methods are abstract you HAVE to declare the class abstract but you MAY declare it abstract also when it's fully concrete. So an abstract class is one that's DECLARED abstract.
  • 12. Re: what is the difference between an interface and an abstract class?
    807598 Newbie
    Currently Being Moderated
    diffrence between interface and class
  • 13. Re: what is the difference between an interface and an abstract class?
    807598 Newbie
    Currently Being Moderated
    diffrence between interface and class
    All this discussion didn't help???????????
  • 14. Re: what is the difference between an interface and an abstract class?
    807598 Newbie
    Currently Being Moderated
    diffrence between interface and class
    They're both types (you can declare variables of them). The only difference in principle is that a class can carry implementation wheras an interface can not, AND that you can inherit from one class only but from as many interfaces you wish.
1 2 3 Previous Next