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
Joined: Jan 20, 2006
Posts: 13
posted
0
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)
Janaranjani<br />SCJP 1.5(97%)
Ilja Preuss
author
Sheriff
Joined: Jul 11, 2001
Posts: 14112
posted
0
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.
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Hemant Agarwal
Ranch Hand
Joined: Nov 21, 2005
Posts: 138
posted
0
Abstract class can have a constructor, Interface cannot. Abstract class can have static methods, Interface cannot.
Ilja Preuss
author
Sheriff
Joined: Jul 11, 2001
Posts: 14112
posted
0
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
Joined: Jan 26, 2006
Posts: 33
posted
0
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
Joined: Jul 11, 2001
Posts: 14112
posted
0
Regarding static methods, frankly I don't know.
But what would a constructor in an interface be good for?
rehans oberoi
Ranch Hand
Joined: Dec 06, 2005
Posts: 174
posted
0
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
Joined: Jan 30, 2000
Posts: 18670
posted
0
[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 ]
"I'm not back." - Bill Harding, Twister
Jeff Albertson
Ranch Hand
Joined: Sep 16, 2005
Posts: 1780
posted
0
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.
There is no emoticon for what I am feeling!
Ilja Preuss
author
Sheriff
Joined: Jul 11, 2001
Posts: 14112
posted
0
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
Joined: Dec 06, 2005
Posts: 174
posted
0
what are the main differences between interface and abstract class
Jim Yingst
Wanderer
Sheriff
Joined: Jan 30, 2000
Posts: 18670
posted
0
[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
Joined: Jul 11, 2001
Posts: 14112
posted
0
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?