Meaningless Drivel is fun!
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
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Abstract Class & Interface" Watch "Abstract Class & Interface" New topic

Abstract Class & Interface

Luv Bhatia

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

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

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:
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!

Java API Documentation
The Java Tutorial
I agree. Here's the link:
subject: Abstract Class & Interface
It's not a secret anymore!