This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Rust Web Development and have Bastian Gruber on-line!
See this thread for details.
Win a copy of Rust Web Development this week in the Other Languages forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Abstract class

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi folks,
When do we use abstract class and when do we use interface?
What are the advantages of using Abstract class?
Can i use an abstract class with only method declarations instead of interface..
Bye
Vinay
 
Ranch Hand
Posts: 221
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Vinay,
Use an abstract class when you want to implement some but not all of its behavior, and especially if your intent is that subclasses will define only a small subset of the behavior. If you just want to make a specification with no implementation, it's better to use an interface.
Concrete reason: When you use an abstract class, subclasses can't extend any other class, which puts a real restriction on architecture solutions.
Philosophical (and more important) reason: It's good design for subsystems to interact by programming to each other's interfaces, and the use of a Java <code>interface</code> enforces this choice. It reduces coupling between subsystems.
jply
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Another option and one that is used frequently in the jdk api is to define both an interface and an abstract implementation of it ... so you get the best of both worlds ... you have both a pure interface and a class that implements a lot of the base functionality in a sensible default manner.
Code that makes use of your objects will use the interface, but you can create subclasses by extending the abstract class without having to redefine all the base functionality in each subclass.

 
Kumar Vinay
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanx a lot
Bye
vinay
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic