• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

diff between abstract class and inheritance

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
On top of spaghetti all covered in cheese, there was this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic