interface and abstract class

Discussion in 'Java' started by sandy, May 21, 2007.

  1. sandy

    sandy Guest

    what is the main difference b/w interface and abstract class.
    in which situation we will use choose interface and abstract class
     
    sandy, May 21, 2007
    #1
    1. Advertising

  2. sandy

    Lew Guest

    sandy wrote:
    > what is the main difference b/w interface and abstract class.
    > in which situation we will use choose interface and abstract class


    <http://java.sun.com/docs/books/tutorial/java/IandI/index.html>

    --
    Lew
     
    Lew, May 21, 2007
    #2
    1. Advertising

  3. "sandy" <> wrote in message
    news:...
    > what is the main difference b/w interface and abstract class.
    > in which situation we will use choose interface and abstract class
    >

    Generally abstract classes are used for inheritance of (at least some)
    implementation.
    Interfaces are used for inheritance of type.

    Another thing to note is that, in Java, you can extend only one class while
    you can implement multiple interfaces.

    Of course this is a massive simplification, there are many other factors to
    take into account, mostly to do with OO design.
     
    Richard Reynolds, May 21, 2007
    #3
  4. On May 21, 10:42 pm, sandy <> wrote:
    > what is the main difference b/w interface and abstract class.
    > in which situation we will use choose interface and abstract class


    To add my own 2 cents..

    Interfaces define ONLY abstract methods i.e. methods with no
    implementation.
    Abstract classes can define abstract methods and concrete methods i.e.
    methods with an implementation.

    A class EXTENDS an abstract class.
    A class IMPLEMENTS an interface.

    A class can only extend one class, but can implement many interfaces.

    Rob
    :)
     
    Robert Mark Bram, May 21, 2007
    #4
  5. sandy

    Tom Hawtin Guest

    Richard Reynolds wrote:
    >
    > Another thing to note is that, in Java, you can extend only one class while
    > you can implement multiple interfaces.


    That's not necessarily to say that makes interfaces better than abstract
    classes. Using an abstract class allows the compiler to detect
    impossible casts. Multiple inheritance (of interface) tends to indicate
    bad design (IMO).

    In reality, (pure) abstract class or interface makes no significant
    difference to client code. The main advantage of interface is that the
    code is cleaner (doesn't require public abstract on methods of public
    static final on fields). It also signals an intent that the type is
    contains no implementation.

    Tom Hawtin
     
    Tom Hawtin, May 22, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matthias Kaeppler
    Replies:
    1
    Views:
    458
    R.F. Pels
    May 22, 2005
  2. Sameer
    Replies:
    4
    Views:
    613
    Roedy Green
    Aug 31, 2005
  3. Uzytkownik
    Replies:
    3
    Views:
    601
    Uzytkownik
    Apr 3, 2005
  4. Replies:
    8
    Views:
    546
  5. Replies:
    4
    Views:
    841
    Rolf Magnus
    May 17, 2006
Loading...

Share This Page