Hi, Can anyone please tell me the difference between interface and abstract classes if my abstract class contains only abstract methods? Is it something like interfaces are used to implement multiple inheritance? Thanks, Sachin
1) When you declare an interface, the name of the interface is preceded by the keyword interface, it should not be declared abstract. You must use abstract keyword to declare an abstract class. 2) The methods in an interface are not implemented just like abstract methods in abstract class, but they can't be declared abstract. 3)An interface can extends several interfaces(multiple interface hierarchy), a class(including abstract class) can extends only one another class(linear implementation inheritance), implements interfaces(multiple interface inheritance).
Valentin, Abstract classes can have constructors.I have a doubt here.It is also true that abstract classes can't be instantiated,that is if a class is declared as abstract no object of that class can be created...Then what is the use of having constructors in abstract classes? Veena
SCJP1.4
"Continuous effort - not strength or intelligence - is the key to unlocking our potential."
*Winston Churchill
Another thing, as Thomas pointed out, classes (abstract or not) have a state (instance variables). Interfaces are only allowed to declare constants (static final). Whenever you are in doubt whether you should take an interface or abstract class approach, just ask yourself whether your type under design should be able to hold some state. If the answer is yes, then the abstract class is the correct choice, otherwise an interface could be just fine.
July's JavaPro published James Cooper's review for "Design Patterns Java Workbook", and the very question happened to be one of the "challenges": "Write down three differences between abstract classes and interfaces in Java". Here is what James Cooper came up with: "1) Abstract classes may have some executable methods and methods left unimplemented. Interfaces contain no implementation code. 2) A new class can inherit from an abstract class, but not also from another class. A new class can inherit from any class and still implement one or more interfaces 3) Interfaces do not specify constructors." And here is what the book lists: "1) a class can implement any number of interfaces, but subclass at most one abstract class. 2) an abstract class can have nonabstract methods. All methods of an interface are abstract. 3) An abstract class can have instance variables. An interface cannot. 4) An abstract class can define constructor. An interface cannot. 5) An abstract class can have any visibility: public, protected, private or none (package). An interface's visibility must be public or none (package). 6) An abstract class inherits from Object and includes methods such as clone() and equals().
Originally posted by Mapraputa Is: July's JavaPro published James Cooper's review for "Design Patterns Java Workbook", and the very question happened to be one of the "challenges": "Write down three differences between abstract classes and interfaces in Java". Here is what James Cooper came up with: "1) Abstract classes may have some executable methods and methods left unimplemented. Interfaces contain no implementation code. 2) A new class can inherit from an abstract class, but not also from another class. A new class can inherit from any class and still implement one or more interfaces 3) Interfaces do not specify constructors." And here is what the book lists: "1) a class can implement any number of interfaces, but subclass at most one abstract class. 2) an abstract class can have nonabstract methods. All methods of an interface are abstract. 3) An abstract class can have instance variables. An interface cannot. 4) An abstract class can define constructor. An interface cannot. 5) An abstract class can have any visibility: public, protected, private or none (package). An interface's visibility must be public or none (package). 6) An abstract class inherits from Object and includes methods such as clone() and equals().
I believe there are some words missing from item five. A top-level class or non-nested class may be declared with public or package access, but not protected or private. I believe item five should say that the methods of an abstract class (not the class itself) might have any visibility. The comments about the visibility of methods declared within an interface are also not correct. The methods of an interface are always implicitly public and abstract even if the modifiers are omitted. Therefore, it is not possible to declare a non-public method in an interface. Although the compiler will accept method declarations within an interface with or without the public modifier, the presence or absence of the modifier makes no difference.
Dan Chisholm<br />SCJP 1.4<br /> <br /><a href="http://www.danchisholm.net/" target="_blank" rel="nofollow">Try my mock exam.</a>