Results 1 to 5 of 5
  1. #1
    b4viral is offline Member
    Join Date
    Jul 2012
    Posts
    17
    Rep Power
    0

    Exclamation Why Interface when we have Abstract Class

    hi,

    can anyone tell me one thing i am confused about??..

    in java we have Interface as well as abstact class funda.
    i know most of the basic differences between Interface and abstract class but i am getting confused for one thing.
    why do we use Interface if we can use Abstract class to achieve the same functionality...?

    i know this question can have many answer but i want the answer as a designer point of view. please guide

    thanks in adv

    -vj

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default Re: Why Interface when we have Abstract Class

    why do we use Interface if we can use Abstract class to achieve the same functionality...?
    It doesn't achieve the same functionality. Here's an example: you can only extend a single class - you can implement as many interfaces as you wish. Abstract classes typically provide a base implementation, interfaces provide a zero implementation contract.

  3. #3
    b4viral is offline Member
    Join Date
    Jul 2012
    Posts
    17
    Rep Power
    0

    Default Re: Why Interface when we have Abstract Class

    yes, i agree and understand, but as a designer when shall i use interface and when an anstract calss?

    i mean at orgenization level when we get customer requirement and decide to design basic structure of code when to use an abstract class and when to use an interface?

    see if there are 5 methods i can do following

    create an interface having all 5 methods and at the same time can also can create abstract class having same, while abstract class will give me freedom to have non public members plus non final variables plus i can implement any method if i want and can get additional benifit saving my time not implementing the same method in all child classes.

    what i want to know is an example where i can proove that in such condition an interface defined will work more conveniently and efficiently compare to abstract class.

    thanks

    -vj

  4. #4
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default Re: Why Interface when we have Abstract Class

    When designing, think about what may (or may not) change. Things that could (or will) change are best encapsulated away - often through the use of an interface (doing makes the code much more maintainable and reusable). Think about an algorithm that takes some input and creates some output. Defining the input as an interface allows the algorithm the flexibility to operate on any class so long as it implements the interface. Constraining the input to a class - abstract or not - limits the ability of the algorithm. A good example is the Collections.sort method, which sorts a List of Comparable (interface) - it was written with the idea that the sorted class type will change, to the extent that the sorted algorithm can sort anything so long as they provide an implementation of Comparable.

    If you have not yet, I would recommend reading up on design patterns as they provide lots of examples where interfaces or abstract classes come in handy, and where each is appropriate.

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: Why Interface when we have Abstract Class

    In that instance, interfaces everytime.
    They'll end up being interfaces when you write the code anyway.
    Stuff that is non-public is irrelevant when you are designing this stuff.
    Any design that goes down into the inner workings of individual classes is over-engineered in any case. Define the contract that a class has (ie its interface) and leave it at that.

    That is for the guts of an app (the controller part).
    For the model you'll be working with straight classes anyway.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. When to use Abstract class and Interface?
    By vivekjan17 in forum New To Java
    Replies: 1
    Last Post: 03-05-2012, 10:37 AM
  2. Interface and abstract class
    By vivekbarange in forum New To Java
    Replies: 8
    Last Post: 07-12-2011, 01:22 PM
  3. Replies: 6
    Last Post: 02-11-2009, 10:51 AM
  4. Interface and Abstract Class
    By kian_hong2000 in forum New To Java
    Replies: 1
    Last Post: 08-27-2008, 02:22 PM
  5. Interface Vs Abstract Class
    By javarishi in forum New To Java
    Replies: 5
    Last Post: 06-15-2008, 05:43 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •