aspose file tools*
The moose likes Beginning Java and the fly likes Interface and Abstract Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Interface and Abstract" Watch "Interface and Abstract" New topic
Author

Interface and Abstract

K Elangovan
Greenhorn

Joined: Jan 17, 2003
Posts: 6
Please explain the exact differnce between Interface and Abstract, other than the explanation like method declaration and object instance. Also please let me get the idea , when we go for interface and when for Abstract?
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Basically, there isn't much of a difference between the two, at least as far as I see it. I'm sure there are quite a few technical differences, though. The main difference is that you can only extend ONE class (whether it is abstract or not). However, you can implement as many interfaces as you want. Interfaces are Java's way of allowing a form of multiple inheritance.
Most of the interfaces I use in my code are from the Java API. I seldom create my own interfaces. As noted above, these rare occasions are usually where I need to inherit from two different classes. I think the Java API gives some great examples for when interfaces are appropriate. As you become more familiar with the API, you will see when you should use them in your own code.
HTH
Layne


Java API Documentation
The Java Tutorial
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
An abstract class can have non-abstract methods. An interface can only have abstract methods.


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Kathy Sierra
Cowgirl and Author
Ranch Hand

Joined: Oct 10, 2002
Posts: 1572
Howdy -- if you consider this more as a "inheritance vs. interface" question, it might help to think about polymorphism!!
I'm using the term polymorphism, in this context, to mean, "The ability to refer to an object in different ways" (in other words, using different *reference variable types*).
Dog d = new Dog(); // treat Dog like a Dog
But if you say:
Animal a = new Dog(); // treat Dog like an Animal
(assume Dog extends Animal)
This lets you send a Dog into any method that uses Animal as its declared argument type.
class Vet {
public void giveShot(Animal a) {
// can take a Dog or Cat or Horse or...
}
}
You can probably imagine that for most applications, the Animal class would be abstract.
But what if you have a class that wants to take Pet objects in its methods... including those that are NOT necessarily subclasses of Animal? Like, what if you have a VirtualPet? (like that Sony robot Dog, or a Tomogatchi (forgive my spelling).
In that case, you would need methods that use polymorphic argument types that are INTERFACES rather than classes.
class PetShop {
public void addPet(Pet p) {
// takes anything that implements Pet, regardless of the inheritance tree that thing comes from)
}
}

interface Pet { ... }
class Dog extends Animal implements Pet { ... }
class RoboDog extends Robot implements Pet { ...}
class VirtualCat extends Simulation implements Pet { ... }
So with abstract classes, you are limiting your use of polymorphism to things that are within the same (and only one) inheritance tree. With interfaces, you can use polymorphism to include things that are from DIFFERENT inheritance trees.
I like to think of inheritance as defining what you ARE. Interfaces, on the other hand, define what you CAN DO.
Like the different hats you can wear.
You can see how important that might be for things like Serializable, where nearly ANY class in Java might (and usually SHOULD) implement Serializable.
Technically, as Thomas and others pointed out, abstract classes can have non-abstract implementations where interfaces cannot. But this is not an issue in most designs, because your interface methods would nearly always need to be overridden anyway, in order to make any sense. For example, if the Pet interface has a beFriendly() method, the way a Dog, Cat, Horse, and VirtualPet demonstrate 'friendliness" is certainly going to be unique to that particular Animal (or Robot) type.
Cheers
Kathy extends Human implements Cowgirl, IceCreamEater, Runnable, Sleepable
chad stevens
Ranch Hand

Joined: Aug 20, 2002
Posts: 88
Hello.
This is a good question. I had an interview last week and to this question I answer what Layne did and the interviewer didn't seem enough pleased. He said it wasn't what he was looking for and then started to talk about hierarchies. Maybe he was talking about the uses of one over the other, still not a clear issue. In my work projects, the senior programmers decided to use many abstract classes. It worked fine but I rarely saw any interfaces?? I wonder why? Maybe the interviewer was looking for answers that were more practical issues. I believe this is what we need to brush on.
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
I like Kathy's explanation. It gives more details to what I meant about multiple inheritence.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Well, I used Laynes explanation once in an interview - and it was excactly what the interviewer wanted to hear...


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
Jyothi prakash alluri
Greenhorn

Joined: Jan 19, 2003
Posts: 2
If there is a class called X. If X is having atleast one abstract method, then that class X must be declared as an abstract class. If any class is subclass of X, then that sub class must define that abstract method (giving implementation details) in that sub class. Otherwise that class should also be declared as an abstact class.
If you declare a class with all abstract methods (totally of abstract methods), then you must declare that class as an interface. Those class which declare subclass of this interface, must give implementation details of all the methods declared in interface.
When to use extend or implement.
You can extend only one class but you can implement as many as you want. Only abstract classes are extended. Interfaces are always implemented. But you can extend interface also.
Any corrections in my opinion...
welcome all coments.....
Nayanjyoti Talukdar
Ranch Hand

Joined: Feb 12, 2002
Posts: 71
That's really a very good question..which one to use, Interface or Abstract class?
I think, reusablity is the main criterion to go for Interface or Abstract class. Suppose, for u'r given problem, u have sorted out all the functionalities and attributes, if some of the functionalities are common for all the implementing classes and some of them are specific to that particular class. Then obviously, u should go for abstract class and define all those functionalities as abstract methods.
If all the functionalities of the given problem are common for all implementing classes, then use Interface and implement that in u'r classes.
This is what I think...
---------
Nayan.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Jyothi prakash alluri:
If you declare a class with all abstract methods (totally of abstract methods), then you must declare that class as an interface.

No, you don't have to - it's totally legal to have a "purely abstract class".
It most probably would be a good idea to make it an interface, though...
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Jyothi prakash alluri:
If there is a class called X. If X is having atleast one abstract method, then that class X must be declared as an abstract class.

You can also declare a class as abstract without it having any abstract method. You can do this to prevent this class from being instanciated.
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Originally posted by Jyothi prakash alluri:
If you declare a class with all abstract methods (totally of abstract methods), then you must declare that class as an interface.

This is not true. You can declare the class as abstract. The only restriction is that if you declare one (or more) method(s) abstract then the class MUST be abstract. This is the only condition the compiler will check for (in the context of this discussion), yet there are many other possibilities where you can use the abstract keyword. A class with all abstract methods can be either abstract or an interface. Also, an abstract class can be declared abstract even if none of the methods are abstract.
Keep coding!
Layne
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Originally posted by Ilja Preuss:
You can also declare a class as abstract without it having any abstract method. You can do this to prevent this class from being instanciated.

For those who doubt that you would ever do this, check the Adapter classes that are part of the AWT event model. They are all abstract even though they have no abstract methods.
chad stevens
Ranch Hand

Joined: Aug 20, 2002
Posts: 88
Thomas, you have a valid point. However, then why would you have interfaces if you can get away with abstract classes shown as in your wrapper class example? The answer to that question would probably clear things up for many of us. Like I said, during my interview the person was babbling on about some hierarchy in abstract classes you can't achieve with interfaces. I am not sure I heard right, his accent was too heavy and his english not too good!
Maybe I should state, in which practical example would you use abstract over interface and vice-versa and WHY? I would like to know the answer to this with real life examples. That would be great!
K Elangovan
Greenhorn

Joined: Jan 17, 2003
Posts: 6
Great explanation Kathy Sierra. Happy and thanks for wonderful sharing.
K Elangovan
Greenhorn

Joined: Jan 17, 2003
Posts: 6
Great explanation Kathy Sierra. Happy and thanks for wonderful sharing.
Cheers,
K.Elangovan
Nayanjyoti Talukdar
Ranch Hand

Joined: Feb 12, 2002
Posts: 71
Interface is primarily the replacement of Multiple inheritance in Java.
-------------
Nayan
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
An interface is used to describe what methods a hierarchy of classes should have without describing how those methods should actually work. An abstract class is normally used when you know of some functionality but not all.
Let's take a look at the Collection hierarchy for a rela live implementation.
The Collection interface describes the methods that all Collection objects should have. These include:
boolean add(Object)
void clear( )
boolean contains(Object)
boolean isEmpty( )
Iterator iterator( )
boolean remove(Object)
int size( )
Object[] toArray( )
Any class that implements the Collection interface must have these methods.
The AbstractCollection class implements this interface and provides a fairly basic implementation of the Collection interface without the iterator() and size() methods.
The AbstractList class implements the List interface and extends the AbstractCollection class. It provides a basic implementation of a List. You only need to provide a get() and size() method.
So the interfaces provide the description of the methods and the abstract classes provide partial implementation of the interfaces.
chad stevens
Ranch Hand

Joined: Aug 20, 2002
Posts: 88
Paul that is a great explanation, thank you!
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Another discussion we had in the programmer certification forum about that issue:
difference between interface and abstract classes


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
 
wood burning stoves
 
subject: Interface and Abstract