Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > abstract class versus interface

Reply
Thread Tools

abstract class versus interface

 
 
Rajarshi Guha
Guest
Posts: n/a
 
      02-06-2004
Hi,
I've been brushing up on Java and had a question regarding use of
abstract class and interfaces.

As I understand an abstract class is only useful if I want to use it as a
superclass since it cannot be instantiated. However I also see that an
interface also essentially serves the same purpose in that it cannot be
instantiated and is generally used as a superclass in hierarchies.

So what *is* the difference between these two constructs? And why would
I prefer one over the other?

Thanks,


--
-------------------------------------------------------------------
Rajarshi Guha <(E-Mail Removed)> <http://jijo.cjb.net>
GPG Fingerprint: 0CCA 8EE2 2EEB 25E2 AB04 06F7 1BB9 E634 9B87 56EE
-------------------------------------------------------------------
Q: What's purple and commutes?
A: An abelian grape.

 
Reply With Quote
 
 
 
 
Matt Humphrey
Guest
Posts: n/a
 
      02-06-2004

"Rajarshi Guha" <(E-Mail Removed)> wrote in message
news(E-Mail Removed) ...
> Hi,
> I've been brushing up on Java and had a question regarding use of
> abstract class and interfaces.
>
> As I understand an abstract class is only useful if I want to use it as a
> superclass since it cannot be instantiated. However I also see that an
> interface also essentially serves the same purpose in that it cannot be
> instantiated and is generally used as a superclass in hierarchies.
>
> So what *is* the difference between these two constructs? And why would
> I prefer one over the other?


An abstract class can have method implementations and instance variables
that will be common to all classes. Also, don't forget that an interface
can also be instantiated (in a manner of speaking) on the fly via an
anonymous class, as in:

Runnable r = new Runnable () {
public void run () {
// Do run stuff here
}};

Cheers,
Matt Humphrey http://www.velocityreviews.com/forums/(E-Mail Removed) http://www.iviz.com/


 
Reply With Quote
 
 
 
 
Tony Morris
Guest
Posts: n/a
 
      02-06-2004
Also, don't forget that an interface
> can also be instantiated (in a manner of speaking) on the fly via an
> anonymous class, as in:
>
> Runnable r = new Runnable () {
> public void run () {
> // Do run stuff here
> }};
>


No it can't - an interface can never be instantiated.
This is an anonymous inner class that implements the Runnable interface.
In contrast, this is similar to a named inner class that implements the
Runnable interface, which has nothing to do with "instantiating an
interface". The interface itself was never instantiated.

Anonymous inner classes are not restricted to being used with interfaces.
--
Tony Morris
(BInfTech, Cert 3 I.T., SCJP[1.4], SCJD)
Software Engineer
IBM Australia - Tivoli Security Software
(2003 VTR1000F)


 
Reply With Quote
 
Tony Morris
Guest
Posts: n/a
 
      02-06-2004

"Rajarshi Guha" <(E-Mail Removed)> wrote in message
news(E-Mail Removed) ...
> Hi,
> I've been brushing up on Java and had a question regarding use of
> abstract class and interfaces.
>
> As I understand an abstract class is only useful if I want to use it as a
> superclass since it cannot be instantiated. However I also see that an
> interface also essentially serves the same purpose in that it cannot be
> instantiated and is generally used as a superclass in hierarchies.
>
> So what *is* the difference between these two constructs? And why would
> I prefer one over the other?
>
> Thanks,
>
>
> --
> -------------------------------------------------------------------
> Rajarshi Guha <(E-Mail Removed)> <http://jijo.cjb.net>
> GPG Fingerprint: 0CCA 8EE2 2EEB 25E2 AB04 06F7 1BB9 E634 9B87 56EE
> -------------------------------------------------------------------
> Q: What's purple and commutes?
> A: An abelian grape.
>


Generally, an interface defines no implementation, an abstract class
provides partial implementation, and a concrete* class provides complete
implementation. Or another way of thinking about it is that interfaces
provide 0% implementation, abstract classes between 0 and 100% (inclusive)
implementation and concrete classes 100% implementation.

Therefore, interfaces provide no method implementations (all abstract), an
abstract class can provide some method implementations, and some not
(abstract), and a concrete class provides all method implementations. A
common strategy of using all three is to provide a common interface, which
serves as the super type, and abstract class that provides method
implementations that will be common to most subtypes, and concrete classes
that inherit from the abstract class to provide complete implementation.

--
Tony Morris
(BInfTech, Cert 3 I.T., SCJP[1.4], SCJD)
Software Engineer
IBM Australia - Tivoli Security Software
(2003 VTR1000F)


 
Reply With Quote
 
Niels Dybdahl
Guest
Posts: n/a
 
      02-06-2004
> So what *is* the difference between these two constructs? And why would
> I prefer one over the other?


As mentioned abstract classes can contain method implementations and
instance variables.
On the other hand a class can implement more than one interface but it can
only extend one abstract class.

Niels Dybdahl


 
Reply With Quote
 
Dale King
Guest
Posts: n/a
 
      02-06-2004
"Tony Morris" <(E-Mail Removed)> wrote in message
news:bvv3ac$bve$(E-Mail Removed)...
> Also, don't forget that an interface
> > can also be instantiated (in a manner of speaking) on the fly via an
> > anonymous class, as in:
> >
> > Runnable r = new Runnable () {
> > public void run () {
> > // Do run stuff here
> > }};
> >

>
> No it can't - an interface can never be instantiated.
> This is an anonymous inner class that implements the Runnable interface.
> In contrast, this is similar to a named inner class that implements the
> Runnable interface, which has nothing to do with "instantiating an
> interface". The interface itself was never instantiated.
>
> Anonymous inner classes are not restricted to being used with interfaces.



I think you missed the phrase "(in a manner of speaking)".

But I'm not sure I see Matt's point because you can do the same thing with
an abstract class.
--
Dale King


 
Reply With Quote
 
Matt Humphrey
Guest
Posts: n/a
 
      02-06-2004

"Dale King" <kingd[at]tmicha[dot]net> wrote in message
news:(E-Mail Removed)...
> "Tony Morris" <(E-Mail Removed)> wrote in message
> news:bvv3ac$bve$(E-Mail Removed)...
> > Also, don't forget that an interface
> > > can also be instantiated (in a manner of speaking) on the fly via an
> > > anonymous class, as in:
> > >
> > > Runnable r = new Runnable () {
> > > public void run () {
> > > // Do run stuff here
> > > }};
> > >

> >
> > No it can't - an interface can never be instantiated.
> > This is an anonymous inner class that implements the Runnable interface.
> > In contrast, this is similar to a named inner class that implements the
> > Runnable interface, which has nothing to do with "instantiating an
> > interface". The interface itself was never instantiated.
> >
> > Anonymous inner classes are not restricted to being used with

interfaces.
>
>
> I think you missed the phrase "(in a manner of speaking)".
>
> But I'm not sure I see Matt's point because you can do the same thing with
> an abstract class.


Actually, I was unaware at the time that anonymous classes could subclass
other classes at all--I had only ever seen and used them with interfaces.
My point was only that it is not necessary to have a named class in order to
get an object that implements a particular interface.

Thanks for the civility,
Matt Humphrey (E-Mail Removed) http://www.iviz.com/


 
Reply With Quote
 
Rick Osborn
Guest
Posts: n/a
 
      02-07-2004
I know 100 hiring managers are going to
hate me. But this is a POPULAR tech screen question.
Possibly where you heard it from. I did.

An abstract class is more for design patterns and frameworks.

An interface, more loosely defined underlying components. Or if you
won't
know the underlying implementation/implementor. (i.e. the Math
interface
to OS)

Also, as we all know, an object can only inherit ONE class
at a time, and a plethora of interfaces. (i.e. extending Thread vs.
implementing Runnable)


There's more options, but that's just a few.




Rajarshi Guha <(E-Mail Removed)> wrote in message news:<(E-Mail Removed) m>...
> Hi,
> I've been brushing up on Java and had a question regarding use of
> abstract class and interfaces.
>
> As I understand an abstract class is only useful if I want to use it as a
> superclass since it cannot be instantiated. However I also see that an
> interface also essentially serves the same purpose in that it cannot be
> instantiated and is generally used as a superclass in hierarchies.
>
> So what *is* the difference between these two constructs? And why would
> I prefer one over the other?
>
> Thanks,

 
Reply With Quote
 
Tony Morris
Guest
Posts: n/a
 
      02-08-2004
> I think you missed the phrase "(in a manner of speaking)".

No I didn't miss the phrase. I was clarifying what was obviously a
misunderstanding.

Consider the following:

// This does NOT instantiate an interface
// Case A
class X implements I
{
void m()
{
I i = new X();
}
}

// And neither does this
// Case B
I i = new I()
{
void m()
{

}
};

The fact that there is a named class (called X) in case A (which I assume
the original "misunderstander" agrees that this is not "instantiating an
interface") and an anonymous class for case B is completely irrelevant to
the fact (instantiaing an interface).
This fact is clearly stated in the Java 2 Language Specification and is also
thoroughly examined in the Sun Certified Programmer for the Java 2 Platform
examination.

--
Tony Morris
(BInfTech, Cert 3 I.T., SCJP[1.4], SCJD)
Software Engineer
IBM Australia - Tivoli Security Software
(2003 VTR1000F)


 
Reply With Quote
 
Tony Morris
Guest
Posts: n/a
 
      02-08-2004

> But I'm not sure I see Matt's point because you can do the same thing with
> an abstract class.


You can define an anonymous class of any legal supertype (abstract class,
non-abstract class, interface, etc.)


--
Tony Morris
(BInfTech, Cert 3 I.T., SCJP[1.4], SCJD)
Software Engineer
IBM Australia - Tivoli Security Software
(2003 VTR1000F)


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Abstract Class versus an Interface, when no Members in Common KevinSimonson Java 10 11-09-2011 04:58 AM
Re: Mozilla versus IE versus Opera versus Safari Peter Potamus the Purple Hippo Firefox 0 05-08-2008 12:56 PM
equal? versus eql? versus == versus === verus <=> Paul Butcher Ruby 12 11-28-2007 06:06 AM
what is the difference between abstract class and pure abstract class? skishorev@yahoo.co.in C++ 4 05-17-2006 08:07 AM
Deriving abstract class from non-abstract class Matthias Kaeppler Java 1 05-22-2005 01:28 PM



Advertisments