Difference between Abstarct Class and Interface
HI,
Here is a simple one for the gurus but quite important for me.
What is the difference b/w an Interface and an abstract class?
Why do we need each one of them?
I would appreciate if you people can give examples of each so that I amy understand fully
Thanks in advance...
[317 byte] By [
alikamran] at [2007-9-26 2:56:41]

I'd like to know too. Someone please help him and me!
Abstract classes can have implementations for methods, non-public members, and non-statci fields. That's why interfaces are sometimes not acceptible.
Classes can implement multiple interfaces but can extend only one class. That's why abstract classes are sometimes not acceptible.
Therefore, you need both!
Believe me I am more confused than before!!!!!!!!
Hi,
lets start with an interface. All it is is (a set of rules to follow), i.e a class(interface) that has a list of methods that have to be implemented. These methods do not have a body as such, just the behaviour that is expected, e.g.
interface doIt {
void doSomeThing(String doneIt);
}
If you implement this interface, then you have to also use this method in the implementing class. If you don't then that class must be declared as an abstract class and is not a concrete class until another class implements that class and finally implements all methods.
Yeh, I know, it's really confusing.
I spent four days on a Java course, and was lucky to have (1) a good instructor
(2) Learning JAVA (O'Reilly) Niemeyer & Knudsen.
Brilliant, it answered the question, the interface and et al is to ensure some structured programming that we don't make up as we go along. Sorry, I'm waffling now. Hope this helps.
best kev
In general, you cannot turn an interface into an abstract class. If a class that implements that interface already has a superclass, it would not be able to extend the new abstract class.
In general, you cannot turn an abstract class into an interface. If the abstract class has any method implementations, non-public members, or non-static fields, it cannot be an interface.
Got it?
A normal class (not abstract) has a special behaviour, like java.awt.Frame
. A Frame is a frame no matter how you subclass it. If you create a subclass it will still be able to be displayed by calling it's show
method. So by using a normal class you can create a type of class.
An abstract class does the same, but it leaves some of the code unwritten. For instance java.lang.Number
. This class is abstract becuase it has no knowledge of how to store the number in memory. But it knows that it is a number, and any subclass will still be a number. You could create a subclass of Number that can hold the time in milliseconds and checks your system clock to see what the time is, it could also have methods that return the time in another country, but it would still be just a Number.
An interface is a way to describe what an object can do, not what it is. So with java.lang.Comparable
as an example you can make any class comparable. This means that no matter what type of object you have, it can be compared with other objects. So you can have a subclass of Frame that can be compared with other windows. Or a subclass of Number that can be compared with other Numbers. You can even compare those two different types if you like. So you could compare a window with a number.
That is the difference between abstract classes and interfaces.
I hope you could follow my arguments, it isn't an easy subject,
Daniel
Sorry about the formatting, this was my first attempt at using [ code ] brackets...