This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Interfaces and Abstract Class 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 "Interfaces and Abstract Class" Watch "Interfaces and Abstract Class" New topic
Author

Interfaces and Abstract Class

Venkat Ramanan
Ranch Hand

Joined: Jan 25, 2001
Posts: 30
This is a funda question... will appreciate if anyone could clear the doubt:-)
When will i use JAVA Interfaces and when will i use abstract classes?
Thanks
Shailendra Guggali
Ranch Hand

Joined: Feb 01, 2001
Posts: 86
just a small part of the answer - maybe it is not really a practical answer - but still--
1. For utility purpose - you can extend ONLY ONE abstract class but you can implement any number of Interfaces.
2. Using interfaces you can bring together unrelated entities sharing only that particular feature specified in the interface.
But a extending from a class ties you to the Class features in all respects.
for more details pls
Go to this site for the free download of Java tutorial by Sun Microsystems
http://java.sun.com/docs/books/tutorial/information/FAQ.html#bios
excuse me if i got your question wrong
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
Both Interfaces and Abstract classes force anyone that uses them to implement the abstract methods (that is - put real code there). (Yes, you can make an abstract subclass of an abstract class and not implement ALL the methods - but let's skip that).
The reason you MAKE an interface is to insure that everything that implements it has a consistant set of behavior (they all have methods that match the interface - and maybe more). This means users of the classes that implement your interface have that warm fuzzy feeling knowing that they can use the familiar methods of your interface, even though they are not very familiar with the class using it.
The reason that you might choose to create an abstract class instead of an interface is if you want to provide some methods that already have real code in them (that much less for the next programmer to do) but there are some things that you want to make sure any subclasses can do - but you don't know how to do it for them (it would be so different for each subclass that there is no way to make a common method).
The reason you might choose to IMPLEMENT an interface is that you are advertising to the next guy "You can count on me to do all this stuff". You also might want to have all of the variables that might be in the interface at your fingertips.
Of course sometimes you Implement an interface cuz you are already sub-classing something else and that is the best that you can do.
The reason that you sub-class an abstract class is to take advantage of the code and variables that are already done, as well as guaranteeing the next guy the consistant behavior by implementing the empty methods.


"JavaRanch, where the deer and the Certified play" - David O'Meara
Gaurav Mantro
Ranch Hand

Joined: Sep 08, 2000
Posts: 61
I assume that final methods, variables are allowed in abstract class and not in interface. If that's so then abstract classes make a good choice when there is some basic core functionlaity that I don't want to be changed by sub-classes but being inherited. Similar won't be possible thru interface.

----------------------<br />regards.<br /><a href="http://www.mantrotech.com/editors/gaurav_mantro.asp" target="_blank" rel="nofollow">Gaurav Mantro</a><br /><a href="http://www.mantrotech.com/technology/java/index.asp" target="_blank" rel="nofollow">Java Information Portal</a> - <a href="http://www.mantrotech.com/technology/java/index.asp" target="_blank" rel="nofollow">http://java.mantrotech.com</a>
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
True, interfaces can not have final methods, because all their methods must be empty, and must eventually be implemented somewhere. However they can have variables, and the variables can be final.
But yes, you are right, you can protect your methods by making them final - but often you don't want to. Instead you offer default behavior and allow the sub-class to decide if it has any better stuff to do.
 
jQuery in Action, 2nd edition
 
subject: Interfaces and Abstract Class
 
Similar Threads
Anstract class, Interface real world scenario
Difference from design perspective
Interface
when to use abstract and interfaces in our project
abstract classes vs interfaces