• 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

Abstract Class & Interface

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I couldn't decide on it whether it's too simple or a tough question, so i thought to put it here only.

I just want to know the difference between an abstract class & an interface?
Why we just couldn't use the interfaces instead of abstract classes?

Am really seeking a satisfactory reply for it. Thanks.
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Interfaces contain no implementation at all.

In an abstract class, you can have many methods that are implemented but leave some methods abstract so that different subclasses don't have to redo the work that's been done and they can override the abstract methods for their application.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Welcome to JavaRanch!

The best way to use an abstract class is as a way to partially implement an interface, letting multiple other classes extend it and complete the implementation. One example of this in the Java APIs is the class java.util.AbstractList. A number of other classes extend it, but no-one normally thinks about that. It just provides a sort of "starter implementation" of java.util.List. Other classes fill in the details.
 
Manu Dhanda
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But there are classes which are fully abstract? I mean all of their methods are abstract.
In that situation, what are the reasons to declare them abstract rather then declaring them an interface?

Partially implementing a class is one fact, but stil it's not the actual reason for declaring something abstract. Abstract is a tier that's been put between an interface and a class, but I couldn't see the exact reason for it.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
See also http://faq.javaranch.com/view?InterfaceVsAbstractClass
 
Manu Dhanda
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the link.
I got some useful information there 'n further links for more info.

Cheers..
 
A berm makes a great wind break. And we all like to break wind once in a while. Like this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic