This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Abstract Class Vs Interface Class 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 "Abstract Class Vs Interface Class" Watch "Abstract Class Vs Interface Class" New topic

Abstract Class Vs Interface Class

Kay Liew
Ranch Hand

Joined: Dec 26, 2003
Posts: 112
I have been reading KB and Java Exam Cram Prep. I know their syntax but unsure about their usgage especially they can almost be used interchangeably.

For example, I can define an abstract class as interface class as long as i have ALL abstract methods within the class definition. Am I right about this statement ?

What other differences do they have that Java have to have them both separately ?


Unity can only be manifested by the Binary. Unity itself and the idea of Unity are already two.
Sandeep Jindal
Ranch Hand

Joined: Aug 25, 2003
Posts: 180
Hi Kay,

Your first difference is rite.
But the basic difference between an interface and abstact class is in the definition.
Interface is an agrement between two layers to follow a predifined chanel of communication(I know these are complex words, but this is how a definition is..), and abstarct class is just an abstarct form of a class.

Anways, as per the implementation point of view
- your first difference is very well rite,
- the second difference is the miltiple inheritance, the way java provides multiple inheritence.

Gud luck

Sandeep Jindal
Peter Laurinec
Ranch Hand

Joined: Sep 13, 2004
Posts: 52
Well what I know:

1. If you extends and abstract class, you dont have to implement all the methods
(if you implement an interface you HAVE to)

2.In abstract class you can also provide some non abstract methods(you can have some implemented)

Hepofully im right and this helps you

SCJP 1.5 (done, at last)<br /> <br />"If everything seems to be going well you have obviously overlooked something."
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Do not forget that a class can only extend one other class (abstract or not) whereas it can implement any number of interfaces.

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Kay Liew
Ranch Hand

Joined: Dec 26, 2003
Posts: 112
Thank you everyone. I, in fact, learned something new from each of your post. It summarizes all the stuffs that I read about abstract and imterface where later confused myself

Howewver, if any of you think of something else that are related to the differences between Abstract and Interface class please go on. I might know some of them but it is always better to hear from different perspectives.

Thanks again,
Kay Liew
Ranch Hand

Joined: Dec 26, 2003
Posts: 112
The following information was extracted from

web page

interface I {
void setValue(int val);
int getValue();

Are my following explaination exactly correct? Just wanted to test my concept on this.

Alternatives C to E are completely out of the choice because "implements" requires exact methods defined in the interface class.

But why A is wrong .. ?
Is it because what Barry has mentioned? "Do not forget that a class can only extend one other class (abstract or not) whereas "

[ September 16, 2004: Message edited by: Kay Liew ]
Louie van Bommel
Ranch Hand

Joined: Aug 17, 2004
Posts: 76
Originally posted by Kay Liew:
[QB]But why A is wrong .. ?

A class must "implement" and interface. (not extend it). Here attempting to extend an interface in a class causes a compile error.

A class (whether abstract or concrete) can extend another class (whether concrete or abstract) but it cannot "implement" that other class.

A class (whether abstract or concrete) can implement an interface, but it can't extend it.

An interface can extend another interface, but it can't implement it.

An interface can't extend or implement a concrete class and it can't extend or implement an abstract class.

The two interface methods in your code are implicitly public so any abstract or concrete class that implements one of the methods has to declare that method as public. (All methods in an interface are public, even if you don't specify them as public in an interface).

Also, an abstract class that implements an interface doesn't have to implement ANY of the interface methods. So an abstract class that implements only one (or both, or none) of the two methods is perfectly legal.

One more point. If an interface has something like
int a =3;

The variable a is implicitly public static and final, even though you don't need to specify these modifiers.
Kay Liew
Ranch Hand

Joined: Dec 26, 2003
Posts: 112
Louie van Bommel ,

I am glad that you've responded to this thread. I am englightened :roll:

I agree. Here's the link:
subject: Abstract Class Vs Interface Class
jQuery in Action, 3rd edition