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

Abstract Class vs. Interface

 
Ranch Hand
Posts: 1309
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How do I determine an object should be an abstract class or an interface? Could the explanation be given with examples? Thanks.
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
IMO an Interface must be be preffered to abstract classes unless you wish to have functionality that an Interface does not provide. This would include having non-primitive attributes (you can only define primitive data members in an interface). Also all data members in an Interface are final and static, so that may be another consideration. Also since we cannot implement methods within Interfaces, we must use abstract (non-pure) where we wish to impplement a method in the base class which will be used by the sub-classes.
Since Java does not support multiple inheritence it is always better to use Interfaces wherever possible so that we do not lock up a sub-class's ability to inherit from any other class.
Parag
 
Ranch Hand
Posts: 445
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi JiaPei,
Nice to see you here. I composed a mail just now which invited you to participate in this forum.
 
Sheriff
Posts: 17734
302
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Parag is right about preferring interfaces over abstract classes. The main reason for choosing abstract class over interface, as given by Joshua Bloch in his book "Effective Java Programming Language Guide" is if ease of evolution is more important than flexibility and power.
You can add a method to an abstract class that provides for some basic functionality. Doing the same with an interface would require making changes to all implementing classes and adding the new method. When you have a published interface, one that has been released to the outside world, it would be practically impossible to change it at all.
Contrary to what Parag said earlier though, interfaces can have non-primitive members. Go ahead, try it.
Junilu
 
Doug Wang
Ranch Hand
Posts: 445
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi JiaPei,
Two articles about Abstract classes Vs. Interfaces from JavaWorld you'd like to check out: Q & A and Move from theory to practice on with a complete example.
Junilu, I am not sure I catch the statement in the above Q&A article "If you need to change your design, make it an interface." It sounds contrary to what you said.
Your thoughts?
[ March 28, 2002: Message edited by: Doug Wang ]
 
JiaPei Jen
Ranch Hand
Posts: 1309
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, Doug. The information is very helpful.
 
Look! It's Leonardo da Vinci! And he brought a tiny ad!
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic