Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

diff between abstract class and inheritance

 
madhuchilipi reddy
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Raghavendra Nittur
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic