• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

when to use abstract class and interface

 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can any body explain me when to use abstract class and when to use interface . i know the basic differences b/w them
 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As far as i remember, we can write method bodies into abstract classes:

...
public void printHello() {
System.out.println("Hello");
}
...

But we can not do this in interfaces. So when i have some common code to use between all specialized versions of my classes but also have interface requirements (inherited classes are required to have their own implementation of some methods), i will prefer to use abstract classes.

If there is no common functionality, i will just use an interface.
 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Casper Casper",
Please see the warning about your display name here.

If you do not update your display name soon your account will be deleted

Dave
[Edit - ooops, wrong link]
[ August 10, 2005: Message edited by: David O'Meara ]
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
http://faq.javaranch.com/view?InterfaceVsAbstractClass
 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Simple rule.

* Use interface if you don't want to implement any of the member functions.
This implies.
- The class implementing the interface must implement all of its functions.

* In abstract class you need not define/implement all the functions like interface. This can be used where you can to define/implement some method and allow user to implement the rest(if there any unimplemented funtions..since there an abstract class can define/implement all the functions).

Interface and abstract class can be used for multiple inhertence. (OOP concept)
 
Thanks tiny ad, for helping me escape the terrible comfort of this chair.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic