This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Abstract Class & Interface Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Abstract Class & Interface" Watch "Abstract Class & Interface" New topic
Author

Abstract Class & Interface

Luv Bhatia
Greenhorn

Joined: Apr 01, 2004
Posts: 16
When we should use Interface and When to use Abstract Class.
Bhaskar Chaganti
Greenhorn

Joined: Apr 10, 2004
Posts: 4
Both abstract classes and Interfaces have one goal in common. They should be used when the implementation is not specific. To make things clear let us say you have a 'Shape' class. Shapes could be of different kinds for e.g. a circle, a rectangle, a square etc. If 'Shape' class had a method called 'draw()', during designing you class, you cannot specifically say how this method should be defined. That's because each shape is drawn differently. So you have a draw method for square, a rectangle etc.
Here both abstract classes and interfaces can be used. Now if you want to enforce super-class sub-class relationship between shape and square and other classes you use abstract classes. Whereas in interfaces such relationship is not enforced.
Hope it's clear now..


Thanx for your effort. Keep smiling and enjoy life.
Ajith Kallambella
Sheriff

Joined: Mar 17, 2000
Posts: 5782
Moving to Java In General(beginner) forum..


Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
Elouise Kivineva
Ranch Hand

Joined: Feb 07, 2002
Posts: 154
Generally an interface is implemented to gain methods while a class is extended to gain information (instance variables) and perhaps methods as well.
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
This question has been asked here many times before. Using the search tool, I found the following threads on the subject:
http://www.coderanch.com/t/386670/java/java/Classes-Abstract-Interface
http://www.coderanch.com/t/386847/java/java/Abstract-Interface
http://www.coderanch.com/forums/
http://www.coderanch.com/t/387081/java/java/difference-interface-abstract
http://www.coderanch.com/t/387121/java/java/Abstract-vs-Interface
http://www.coderanch.com/t/387137/java/java/Extends-vs-Implement
http://www.coderanch.com/t/387287/java/java/abstract-class-interface
And those are only from the first dozen matches out of a hundred! I'm sure one of those will answer your questions. If you still don't understand, feel free to use the search tool yourself.
Of course, we are always here to clarify any confusions you may have, so feel free to come back with more questions.
Keep coding!
Layne


Java API Documentation
The Java Tutorial
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Abstract Class & Interface
 
Similar Threads
When to use Interfcae and abstract class
abstract
abstract class vs interface ?
use of abstact class and interface
forcing implementing methods