• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

abstract class(all abstract methods) vs interface

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi guys,

As such i know the basic difference ,but i want toknow what is the differnece between a interface and an abstract class with all the abstract methods in it.

thanks in advance

yash
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Abstract follows Strict Class Hierarchy but Interface doesn't.If a method with in a Abstract class is found to change with time,then it should be put in Interface.So that according to the situation,you can define that method by implementing it.

Methods within an Abstract class can be either Concrete or Abstract.
Whereas Methods within an Interface is always Abstract(and public)
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The only difference between an interface and a fully abstract class is that you can inherit from only one abstract class, but from as many interfaces as you like. In fact, that is the only reason why interfaces exist in Java: to provide for multiple inheritance.

See http://faq.javaranch.com/view?InterfaceVsAbstractClass
 
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Abstract class can have a constructor, Interface cannot.
Abstract class can have static methods, Interface cannot.
 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Hemant Agarwal:
Abstract class can have a constructor, Interface cannot.
Abstract class can have static methods, Interface cannot.



Good points. Interfaces also can't have instance variables (non-static fields) or instance initializers.
 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everyone,

CAn anyone explain why the following happen???

Abstract class can have a constructor, Interface cannot.
Abstract class can have static methods, Interface cannot.

 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Regarding static methods, frankly I don't know.

But what would a constructor in an interface be good for?
 
Ranch Hand
Posts: 174
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the best answer of this questions are

1. java does not provide multiple inheritance .
we can't use extends two times . so interface comes in picture .

2.abstract class can have abstract methods or method with body.
interface has all abstract methods
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[rehans oberoi]: 1. java does not provide multiple inheritance .
we can't use extends two times . so interface comes in picture .


Java does not allow multiple inheritance of implementations (classes). It does allow multiple inheritance of declarations. You can't have a class extend two different classes, but and interfact can extend two different interfaces, and a class cah implement two different interfaces.

Regarding staitic methods in interfaces, I suppose it might have been possible for them to allow this, but I don't see the point. The main idea of an interface is that you have a set of method declarations that can & must be overridden by any instantiable class which implements the interface. If an interface had static methods, then those could not possibly be overridden. (See OverridingVsHiding.) So why bother using an interface if you wanted to do that?
[ January 26, 2006: Message edited by: Jim Yingst ]
 
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Interface I can't have static methods. Class I.C has a static method. I see. C spot run. Run, spot, run.

Excuse, it's that time of the afternoon when I need more coffee, but my point is since nesting is permitted, the restriction against static methods in an interface is a bit silly: you can achieve a similar result with a member class.
 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jim Yingst:
Regarding staitic methods in interfaces, I suppose it might have been possible for them to allow this, but I don't see the point. The main idea of an interface is that you have a set of method declarations that can & must be overridden by any instantiable class which implements the interface. If an interface had static methods, then those could not possibly be overridden. (See OverridingVsHiding.) So why bother using an interface if you wanted to do that?



Sometimes I wish I could do that to provide a utility method that works on instances of the interface. But not often.

By the way, you could ask the same question about static fields, which *are* allowed - a little bit inconsistent, at first sight.

On the other hand, it just occurred to me that allowing static methods would have made the language specification more complex. As it is now, all methods in an interface are implicitely abstract - which doesn't make sense for static methods. So they would have had to make a distinction between static and non-static methods. Just disallowing static methods probably was just simpler.
 
rehans oberoi
Ranch Hand
Posts: 174
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what are the main differences between
interface and abstract class
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[Ilja]: By the way, you could ask the same question about static fields, which *are* allowed - a little bit inconsistent, at first sight.

Yeah. But fields have never been overrideable, whether static or not. And allowing static fields in interfaces has led the constant interface [anti-]pattern, which suggests it maybe wasn't that great an idea to begin with. I think there are some cases where it could make sense to have constants as part of an interface, in cases where those constants are actually of interest to clients of the interface. Most such uses that I can think of are probably best implemented with nested enums nowadays.

Just disallowing static methods probably was just simpler.

Yeah. And people still have access to a wider variety of options using an abstract class, anyway. If they feel it's warranted. Keeping interfaces simple is part of their appeal, I think.
 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by rehans oberoi:
what are the main differences between
interface and abstract class



What was unclear about my first post in this thread?
 
reply
    Bookmark Topic Watch Topic
  • New Topic