jQuery in Action, 2nd edition*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Difference between abstract classes and interfaces Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Difference between abstract classes and interfaces" Watch "Difference between abstract classes and interfaces" New topic
Author

Difference between abstract classes and interfaces

Kalyani Athalye
Greenhorn

Joined: Feb 27, 2003
Posts: 6
Hi All!
I am with a very basic or may be stupid question, but plz answer my query.
I just want to know what are the differences between absract classes and interfaces and why there is need of these two separate entities when they function in similar ways..
Thanks & Regards,
Kalyani
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Please check out the following discussion on the same topic:
difference between interface and abstract classes


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

Joined: Aug 07, 2001
Posts: 418
Hi Kalyani,
Well the abstract cla and the interface , these are 2 diff. entities. and not the same.
1. u can implement some methods in abstract class and u can keep some of them as abstract so that the derived class implements it. In the interface all methods are by default abstarct. u can not provide implementation to any generalised method also. the implementing class HAS TO implement those methods.
2. u can have private, protected instance member variables in abstract class. In the interface all variables are by default public static and final. u can't have private or protected in interface.
3. The interface is java's way to provide multiple inheritance functionality in OOPs. u can not extend 2 abstract classes at a time in java, however, u can very well implement 2 interfaces.
Hope this helps. :-)
Rashmi
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
2. u can have private, protected instance member variables in abstract class. In the interface all variables are by default public static and final.
However, it is a good practice not to see public static final fields as variables, but as constants.
Sarma Lolla
Ranch Hand

Joined: Oct 21, 2002
Posts: 203
Also,
Abstact class can have constructors. Interfaces can't.
For no reason you can specify a fully developed class with abstract.
You can't instanciate an abstract class but you can instantiate a non abstract class derived from abstract class.
You can't instantiate an interface except the non abstract class that implements the interface.
What are we telling you? Difference between an abstract class and interface. What did you ask? Why these two?
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
For no reason you can specify a fully developed class with abstract.
For no reason? Are you sure? What about declaring a fully-developed class abstract in order to prevent people from directly instantiating it?
Sarma Lolla
Ranch Hand

Joined: Oct 21, 2002
Posts: 203
For no reason? Are you sure? What about declaring a fully-developed class abstract in order to prevent people from directly instantiating it?

That is exactly my point. When the class is fully developed why are we using abstract. Any class that extends this fully developed class with {} and without adding a single line of code can be instantiated.
Leandro Oliveira
Ranch Hand

Joined: Nov 07, 2002
Posts: 298
One example of the usage of abstract classes and interfaces can be seen in the collections framework in java.util package. Everything starts with an interface, Collection, then, many abstract classes are declared, these classes do some work that but let part of the work undone (that's why they are declared abstract). Then, many classes are created, they just extend an abstract class. Since part of the work is already done you have less work!!
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
That is exactly my point.
My point was about the "for no reason" part. If you declare a fully-developed class abstract, there is a reason, and the reason is that you don't want people to instantiate it directly.
So your statement would be best rephrased as
"you can intentionally declare a fully developed class abstract if you don't want people to instantiate it directly."
Pickyness is (one of) my vice
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
One example of fully developed abstract classes are the adapter classes in AWT event handling.


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

Joined: Jan 08, 2003
Posts: 35
Hi Valentin,
I got the point of declaring the fully developed class as abstract, so that no one can instantiate it directly.
But you can have another approach for this too.
We can declare the constructor of the class as Private, which will not allow anyone to instantiate that class.
So, any other reason for declaring the fully developed class as abstract ?
Warm Regards
Mandar


To Bug is Human,<br />To Debug Divine... :-))
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Mandar,
If you declare the constructor private, you will have no way of extending the class, since the subclass constructor will not be able to call the supeclass constructor.
Kalyani Athalye
Greenhorn

Joined: Feb 27, 2003
Posts: 6
hi all!
Thanks a lot
Regards,
Kalyani
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Difference between abstract classes and interfaces
 
Similar Threads
Abstract classes and Interfaces
initilisation of classes vs interfaces
What is an abstract class? (was: scjp)
Class Inheritance and Interface Inheritance
Differences betweenabstract classes & interfaces