wood burning stoves 2.0*
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
Author

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 ?



THanks,
k


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

Regards,
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,
K
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 "

Thanks,
k
[ 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:

k
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Abstract Class Vs Interface Class
 
Similar Threads
interface extending a an abstract class
diff b/w abstract class & interface
abstract Vs interface
Diff bw Abstract class and Interface
interface and abstract