Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

abstract class(all abstract methods) vs interface

 
yashpal patel
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Janaranjani K
Greenhorn
Posts: 13
  • 0
  • Mark post as helpful
  • send pies
  • 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)
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Hemant Agarwal
Ranch Hand
Posts: 138
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abstract class can have a constructor, Interface cannot.
Abstract class can have static methods, Interface cannot.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
mert ´┐Żzkaya
Ranch Hand
Posts: 33
  • 0
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Posts: 14112
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Regarding static methods, frankly I don't know.

But what would a constructor in an interface be good for?
 
rehans oberoi
Ranch Hand
Posts: 174
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • 0
  • Mark post as helpful
  • send pies
  • 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 ]
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • 0
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Posts: 14112
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what are the main differences between
interface and abstract class
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • 0
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Posts: 14112
  • 0
  • Mark post as helpful
  • send pies
  • 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?
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic