wood burning stoves 2.0
The moose likes Beginning Java and the fly likes abstract & interface Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "abstract & interface" Watch "abstract & interface" New topic

abstract & interface

jidish kumar

Joined: Sep 05, 2003
Posts: 3
can you give the real diference between abstract & interface.
where all their needs are more useful in java
Wayne L Johnson
Ranch Hand

Joined: Sep 03, 2003
Posts: 399
An "interface" is like a contract: it dictates what behavior needs to be defined, but doesn't provide any implementation. There are no instance variables or methods, but only method signatures (method name, parameter list, return type).
An "abstract class" is a class that can't be instantiated, but it can contain instance variables and methods. It assumes that there will be "concrete" classes [non-abstract] that extend the abstract class.
Abstract classes can be used as parent classes when there is a lot of common function/behavior in the child classes that you want to define in a single place. Interfaces are used when you need to define what methods are to be supported, but there is no common functionality because every class that implements the interface will do it in very different ways.
Good examples of interfaces are found in the "java.sql" package. Sun defines the behavior of a Connection (i.e., "close()", "createStatement()", etc.), but they have no way of know how that behavior is to be implemented for each database. Those details are left up to the driver providers.
The other difference lies in the fact that Java defines a single-inheritance model, meaning a child class can extend only one parent class. However it can implement as many interfaces as it needs to, so you get some of the benefits of multiple inheritance w/out many of the issues.
Interfaces also allow you to easily plug in different classes at run-time, as long as they all implement the target interface.
I'm sure others can explain it better, but if you search the web I'm sure you find many other explanations as well. (Here's a quick one: The Java Boutique).
[ September 17, 2003: Message edited by: Wayne L Johnson ]
Nathan Pruett

Joined: Oct 18, 2000
Posts: 4121

Since this doesn't have anything to do with GUIs or Graphics directly, I'm moving this topic to Java in General.

Write once, run anywhere, because there's nowhere to hide! - /. A.C.
Ilja Preuss

Joined: Jul 11, 2001
Posts: 14112
This is a *very* frequently asked question - doing a search should give you many past discussions on exactly this topic.

The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
I agree. Here's the link: http://aspose.com/file-tools
subject: abstract & interface
It's not a secret anymore!