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

So I’m showing my ignorance here.. I don’t have a CS degree (sometimes I think I should go back to school to get one) so I guess you could say I’m homeschooling myself. So! In Java, what is the difference between an interface and an abstract class? What differences are there between implementing an interface and extending an abstract class?

14 thoughts on “what is the difference between an interface and an abstract class?”

  1. Answering my own question: a class implementing an interface must implement (or override, same thing right?) all the of methods defined in the interface, while a class extending an abstract class need not implement any of the methods defined in the abstract class. Additionally, a class extending an abstract class can implement an infinite number of it’s own methods.

  2. A class implementing an interface can implement other methods too; that’s how you can have one class implement multiple interfaces.

    Which is the big practical difference, in Java: a class could implement multiple interfaces, but could only subclass one abstract class.

  3. What i think is that :
    1) In Abstract class we need not need to override all those methods without abstract,static. where as while inplementing an interface we need to override all the methods.
    if we dont want to override we’ll have to use accesifier abstract.

  4. Dear All,

    I think that in the case of an Interface, we have no other way than provide implementation for all the methods. But in the case of Abstract classes, we can provide implementation for some of the methods and declare the class as ‘abstract’. So we have an intermediate, customised version available for our use.

  5. Definition: An interface is a named collection of method definitions (without implementations). An interface can also declare constants.

    -An interface cannot implement any methods, whereas an abstract class can.
    -A class can implement many interfaces but can have only one superclass.
    -An interface is not part of the class hierarchy. Unrelated classes can implement the same interface.

  6. Actaully interface is just like a protocol(Networking???)which can work as a bridge between two class;a role or specification while in abstract class we put some high level design will be implemented by low level classes as per their requirements.Other difference are the triviasls…like interface can not have method def…1 or more interfces can be implemented but not the same in class blah..blah..blah..

  7. An interface is a contract, which means that all the classes implementing an interface are bound to follow the contract by giving the implementation of the methods. By extending from an abstract class yiou are not bound by any specification that you have to necessarily give an implementation to a particular unimplemented method of the abstract class.

  8. Interface defines contants and method signatures but doesn’t implement any of the methods. Whereas, an abstract class can define some methods and also implement some other methods.

    If you implement an interface you have to provide implementation for all the methods that are defined in the interface.

    If you extend an abstract class you can override some of the methods that are implemented in the abstract class or use them directly but should and must provide implementation for those methods for which you don’t have implementation in abstract class, otherwise your class which extends the abstract class will itself become an abstract class.

    If you want to extend an interface your class which extends it should be an interface.

    Interfaces are very good to use in providing an layer of rules for coding in your application.

  9. I’ll talk layman 🙂
    Consider this hypothesis : An auto parts supplier ABC deals with manufacturing steering wheels for XYZ a major brand of automobile.
    XYZ may have several models all “MAY BE” having different cosmetic desing but following the same standard spec for steering wheel rod
    and other mechanical accessories like wheel bearings etc. Now, vendors like ABC and EFG want to bulk mfg steerings that conform to
    XYZ’s requrirements. XYZ provides them with a spec –> ” Interface ( or contract ) in S/W design parlance”. ABC, EFG or any other vendor
    interested in selling their steering wheels to XYZ should conform to the standard interface that XYZ has provided. In situations where
    ABC falls short of inventory, XYZ can use EFG’s supplied part with relative ease the reason: Both confirm to a standard interface.

    Internally, ABC and EFG might have their own design standards for cosmetics, colors, dyes etc. They can use some basic standard spec that
    can or cannot be inherited by some or all the steering wheels they produce. On general example would be that every steering has a ABC
    inprint or some sort of trademark. This trademarking process is inherited by all the steering wheels no matter what size of spec a.k.a
    “Interface” they conform to. ABC or EFG can mfg steerings conforming to other “Interfaces” provided by some other major brand other than
    XYZ but still keeping the trademarking process consistent. This trademarking process relates to –> ” Abstract class and Inheritance in
    S/W design parlance”.

    Another thing worth noting here is that XYZ may inturn be inheriting or contracting with some other super vendors or other part mfgs and
    thier interfaces leading to inheritance. Same applies to abstract classes, one abstract class extends other and so on and so forth.

    Hope this helps.

  10. An interface is just like a stub to a class or many classes which
    implement the methods that are declared(without implementation) in it. Any class can implement multiple interface’s and the same interface method’s can be implemented in different ways in different classes.
    On contrary an Abstract class can have all or some (atleast one) of its methods
    declared by the keyword ‘abstract’ and may be fully
    or partially implemented,
    so that the child classes extending the parent Abstract
    classes my further enhance the
    method implementation.
    Now, any new class that is
    implementing an interface should have complete method
    implementation, that are declared in the interface.
    When I meant by ‘declared’
    is that you can only see the
    method signature in the interface. Where as, a class
    extending an abstract class
    need not implement all the
    methods, but should definitely
    implement the ‘abstract’ method, otherwise this class would inturn become an abstract class.

  11. An interface can have only abstract methods, where as an abstract class can have both abstract and concrete methods.

  12. – a class can implement any no. of interfaces but a class can’t extend more than one abstract classes.
    -No method can be implemented in a interface, they are only defined while in an abstract class methods may be inplemented.
    -An interface don’t have instance variables while an abstract class may have.
    -variables declared inside an interface declaration are implicitely final and static while nothing such in abst class.
    -All methods of an interface are by default abstract while in an abstract class methods are not abstract by default moreover concrete methods may also be present.
    -Since by default the variables of an interface are final they must be initialized with a contant value while this is not mendatory in case of abstract class.

Comments are closed.