File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes diff between abstract class and inheritance Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "diff between abstract class and inheritance" Watch "diff between abstract class and inheritance" New topic
Author

diff between abstract class and inheritance

madhuchilipi reddy
Greenhorn

Joined: Feb 09, 2007
Posts: 17
hi
i have one doubt.
abstaract class menas it having abstaract methods
interface menas abstaract methtods and constant variable.


we are writing class abstract inthis class we write all methods
abstract and declare variales final it act as a interface or not
.we can write means wt is the diff between abstract and interface
Raghavendra Nittur
Greenhorn

Joined: Feb 19, 2007
Posts: 29
Hi madhu,

You are correct. But there are some major differences between interfaces and abstract classes.

�An Interface can only declare constants and instance methods, but cannot implement default behavior.
�Interfaces provide a form of multiple inheritance. A class can extend only one other class.
�Interfaces are limited to public methods and constants with no implementation. Abstract classes can have a partial implementation, protected parts, static methods, etc.
�A Class may implement several interfaces. But in case of abstract class, a class may extend only one abstract class.
�Interfaces are slow as it requires extra indirection to find corresponding method in the actual class. Abstract classes are fast

Regards, Raghav
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Pretty good list, but I'd ask for proof of this bit: "Interfaces are slow as it requires extra indirection to find corresponding method in the actual class. Abstract classes are fast." I'd never use performance to make the choice between abstract & interface.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Stan James:
...I'd ask for proof of this bit: "Interfaces are slow as it requires extra indirection to find corresponding method in the actual class. Abstract classes are fast." ...

I agree. I'd ask for evidence of this as well.

I'm not convinced it even makes sense. Where exactly is the "extra indirection"? For that matter, how is this different than any polymorphic method call?


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Raghavendra Nittur
Greenhorn

Joined: Feb 19, 2007
Posts: 29
Hi marc/james,

Where good question.

interface MyInterface
{
------
};

class A implements MyInterface
{
-------
};

public class Test
{
A obj = new A();
MyInterface iface; // Reference
iface = obj;
};

Here in this piece of code, iface is a reference to object of class B. So you are using an extra indirection to access methods. This is to Achieve one type of runtime polymorphism in Java.

But this is not the case when we use abstract classes.

Regards, Raghav.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: diff between abstract class and inheritance