• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

why we use interface instead of abstract class

 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI,
Abstract class and interfaces both we have to declare methods. at what situation we can use abstract class and interfaces. both should have to use declared methods. but why mostof the time we prefer interfaces? why not abtstract class.

plz explain any body,
Sanju.
 
Bartender
Posts: 1844
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, Sanju!

Java does not permit multiple inheritance primarily because the designers of the language didn't want it. It it messy to implement and can cause a fair number of errors from programmers who attempt to use it.

It is possible to simulate it, through creative uses of interfaces. Consider:


Also, please read the Javaranch Naming Policy and change your publicly displayed name (change it here) to comply with our unique rule. Thank you.
[ January 05, 2005: Message edited by: Joel McNary ]
 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Whilst interfaces can be used to simulate multiple inheritance (in the C++ sense) that's a mis-use and a bit of a red herring. Interfaces are not there to give you multiple inheritance in Java: if you're doing that then either there's something wrong with your design, the design of some class library or the way you're trying to use it.

To answer the thread starter's question: why use an abstract class instead of an interface (and vice versa)?

I would *always* use an interface, as that defines the set of operations supported by my classes. I might then consider implementing that interface as an abstract class in order to provide some skeleton code or some default behaviour.

Why would I use both?

Because by implementing the top level as an interface I allow for the possibility of anything being overridden: I don't impose any unnecessary restrictions on how the interface is implemented in the future.

I'd use the Abstract class to implement any common implementation: and I'd make it abstract even if I implemented every method defined in the interface because (without going into the reasoning) only the leaf classes of an inheritance hierarchy should be concrete. I wouldn't necessarily have an abstract class at all.

Hope that helps.

Edward
[ January 07, 2005: Message edited by: Edward Kenworthy ]
 
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
why and when we use interface instead of abstract,here my doubt is abstract is more advantage than the interface,we can create abstract methods and somemethod will be left unimplemented,so all point of view abstract is more advantage.but why should we use interface and when we use?recently i faced this question in interview
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sekhar kiran wrote:why and when we use interface instead of abstract


Well, I'm not quite sure why you revived an eight-year old thread to ask the question, but now you have:
  • Interfaces allow multiple inheritance.
  • They allow you to define a type (or, if you prefer, a contract) without any implementation.
  • They allow you to put off decisions that don't need to be made (see the WhatNotHow (←click) page for details - 2nd half).

  • HIH

    Winston
     
    Ranch Hand
    Posts: 449
    Scala IntelliJ IDE Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    sekhar kiran wrote:why and when we use interface instead of abstract,here my doubt is abstract is more advantage than the interface,we can create abstract methods and somemethod will be left unimplemented,so all point of view abstract is more advantage.but why should we use interface and when we use?recently i faced this question in interview


    We also have FAQ about this. Hope it helps.
     
    sekhar kiran
    Ranch Hand
    Posts: 95
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Muhammad Khojaye wrote:

    sekhar kiran wrote:why and when we use interface instead of abstract,here my doubt is abstract is more advantage than the interface,we can create abstract methods and somemethod will be left unimplemented,so all point of view abstract is more advantage.but why should we use interface and when we use?recently i faced this question in interview


    We also have FAQ about this. Hope it helps.


    i explained in interview as the same which mentioned above the link,but he asked ie what are the features in interface will be support in abstract class also.eg interface having public and abstract where it will be available in abstract.so totally abstract is more advantage than interface,then why we use interface?i dont know how to answer to them
     
    Muhammad Khojaye
    Ranch Hand
    Posts: 449
    Scala IntelliJ IDE Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    sekhar kiran wrote: why we use interface?i dont know how to answer to them


    If you read this thread again and the link, I am sure you will get your answer. For instance, Winston replied you with these reasons,

    Interfaces allow multiple inheritance.
    They allow you to define a type (or, if you prefer, a contract) without any implementation.
    They allow you to put off decisions that don't need to be made (see the WhatNotHow (←click) page for details - 2nd half).


    I also recommend you to search about this question in this forum and you will find a lot of similar threads discussing very valuable points.
     
    Winston Gutkowski
    Bartender
    Posts: 10780
    71
    Hibernate Eclipse IDE Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    sekhar kiran wrote:so totally abstract is more advantage than interface


    Then I suggest you start designing a few applications using totally abstract classes.

    Hey, you may be able to get them to work...maybe even well. But hopefully, at some point, you'll run into a big problem and a light-bulb will come on - and then maybe you'll be able to come back to this thread when you're in the mood to listen and do some research of your own.

    Winston
     
    Paper jam tastes about as you would expect. Try some on this tiny ad:
    the value of filler advertising in 2021
    https://coderanch.com/t/730886/filler-advertising
    reply
      Bookmark Topic Watch Topic
    • New Topic