Abstract and Interface

Dear Sir/Madam,Please let me know the difference between Abstract class and Interface.
[100 byte] By [rajneetbhatiaa] at [2007-9-29 16:23:16]
# 1

An abstract class is used to collect generic methods, that can be used by subclasses. In some cases you want the generic methods to call specialized methods, that don't exist yet. You make these specialized methods abstract to ensure that every inheriting class implements it.

An Interface is used to be sure that every implementing class has the correct methods so other classes can call the implementing classes without exactly knowing that class.

Not easy to explain.

Example:

interface Startable{

public void startMe(String aString);

}

class myStartingClass extends Thread implements Startable{

String myName;

public void run(){

System.out.println(myName);

}

public void startMe(String aString){

myName = aString;

this.start();

}

}

And now you can make stuff like this:

public void starterMethod(Startable aStartbl){

aStartbl.startMe("Hello World");

}

public void someOtherMethod(){

aStartbl tmp = new myStartingClass();

starterMethod(tmp);

}

Remember this code has no function, it is only to show a principle.

Seeing that, I'd say, that interfaces can be used to have required methods you can rely on.

tomvollerthuna at 2007-7-15 14:40:27 > top of Java-index,Other Topics,Java Community Process (JCP) Program...
# 2
Why is it that people just ask something an then disappear?
tomvollerthuna at 2007-7-15 14:40:27 > top of Java-index,Other Topics,Java Community Process (JCP) Program...
# 3
I disappeared as i was checking the forums in javaworld.com, but now i will keep this site as main.
rajneetbhatiaa at 2007-7-15 14:40:27 > top of Java-index,Other Topics,Java Community Process (JCP) Program...