• 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

difference between abstract class and interface?

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all, this is my first time in javaranch, hope to get answer to my question, this was asked in a phone interview. Thanks.
 
Ranch Hand
Posts: 348
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
shaggy lype
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for ur reply. I really appreciate it.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
So I left, I came home, and I ate some pie. And then I read 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