aspose file tools
The moose likes Beginning Java and the fly likes difference between abstract class and interface? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Groovy Fundamentals video training course this week in the Groovy forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "difference between abstract class and interface?" Watch "difference between abstract class and interface?" New topic

difference between abstract class and interface?

shaggy lype

Joined: Apr 02, 2004
Posts: 21
Hi all, this is my first time in javaranch, hope to get answer to my question, this was asked in a phone interview. Thanks.
chi Lin
Ranch Hand

Joined: Aug 24, 2001
Posts: 348
my understanding : inside abstract class, you can define
1. non-abstract, non-public method,
2. non-public field.
which are not allowed in interface.
[ April 02, 2004: Message edited by: chi Lin ]

not so smart guy still curious to learn new stuff every now and then
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
A few other differences:
all interfaces are implicitly abstract and can't be protected or private
all interface fields are implicitly public final static
all interface methods must be non-static and are implicitly public abstract
and they can't be final, native, strictfp, or synchronized
a class can extend only one class, abstract or otherwise, but can implement any number of interfaces
[ April 02, 2004: Message edited by: Mike Gershman ]

Mike Gershman
SCJP 1.4, SCWCD in process
shaggy lype

Joined: Apr 02, 2004
Posts: 21
Thanks for ur reply. I really appreciate it.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Interfaces and abstract classes are both used when we want to let someone else know the general type of thing they are working with, but not the exact class. So if I tell you I'm going to pass a List to your method, you know the object will have all the methods and such defined by the List interface but you don't know exactly what class I might send. That gives me the freedom to use an ArrayList, TreeList or whatever best fits my needs without affecting you. You'll see interfaces used this way a lot in the JDK. You can get an Iterator from a List. You are assured that the Iterator implements the Iterator interface, but you don't know exactly what class the Iterator will be.
Abstract classes are similar in that you can't get a real instance of an abstract class. You will always get an instance of something that extends it, but you don't have to know exactly what it is. An extra benefit of abstracts comes when it's your job to extend one. You have to provide concrete implementations of all the abstract methods. But the abstract class may provide some built-in or default behavior to help you out.
For example, I'm working on a server that has many "work classes". The abstract work class has a method "currentUser()" that returns the userid of the person who submitted the request to the server. It's just a little helper method that saves me from writing four or five lines of code every time I need a userid.
Hope that helped!

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
jQuery in Action, 2nd edition
subject: difference between abstract class and interface?