aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes difference between interface and abstract classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "difference between interface and abstract classes" Watch "difference between interface and abstract classes" New topic
Author

difference between interface and abstract classes

Sachin Choudhari
Greenhorn

Joined: Jul 02, 2002
Posts: 10
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
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
abstract classes may also contain constructors which interface can't !!


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
zarina mohammad
Ranch Hand

Joined: Jun 26, 2002
Posts: 104
Is it something like interfaces are used to implement multiple inheritance?

A class can implement more than one interface in contrast to a class which can inherit only a single superclass(abstract or otherwise)
Jie Zhou
Greenhorn

Joined: Jun 06, 2002
Posts: 8
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).
Veena Pointi
Ranch Hand

Joined: Jun 20, 2002
Posts: 442
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
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974

Maybe for the above?


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Veena Pointi
Ranch Hand

Joined: Jun 20, 2002
Posts: 442
Thanks Thomas...
Veena
Sachin Choudhari
Greenhorn

Joined: Jul 02, 2002
Posts: 10
thanks for the reply.
Sachin
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
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.
Khurram Shahood(SCJP2)
Greenhorn

Joined: Jun 08, 2002
Posts: 18
THE DIFFERENCE IS SIMPLE ABSTRACT CLASS CAN HAVE
NON ABSTRACT METHODS BUT INTERFACE ONLY HAVE ABSTRACT METHODS BY DEFAULT.


khurram shahood(Java Farmer)
Sachin Choudhari
Greenhorn

Joined: Jul 02, 2002
Posts: 10
thanks valentin.
I got more clear picture when to use interface and abstract class.
thanks once again.
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
And don't forget, an abstract class doesn't have to have any abstract methods.
Mapraputa Is
Leverager of our synergies
Sheriff

Joined: Aug 26, 2000
Posts: 10065
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().
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
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>
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: difference between interface and abstract classes