> Hi,
> i am new to java. can any one help me to
> lp me to understand the difference between implement
> and extend
No difference, really. They both signify inheritance. The Java language simply requires one in certains situations and the other in other situations.
> Hi,
> i am new to java. can any one help me to
> lp me to understand the difference between implement
> and extend
>
> Thanks in advance
Hope you are'nt finding your replies confusing!
Anyway I'll have a go trying to avoid jargon but it's difficult!
When you implement an interface you promise that your class will have the methods that are required for that interface. They must each have the right name and the right parameters too. The catch is that you must write them!
When you extend a class you don't promise to write any new methods although you can and normally you would. Even when you add methods you can call them anything you want unless you are replacing one which you inherited.
Let me know if you got it now.
> When you extend a class you don't promise to write
> any new methods although you can and normally you
> would.
You promise to provide implementation for any abstract methods, or else decare you new class as abstract, just like you would with an interface. And if the parent class has only abstract methods then the distinction between extending that class and implementing an interface blurs even more.
There's no need for the two different keywords. Both mean effectively the same thing--inheritance--and you could get rid of one or the other and just use one word in all cases where we currently use either one, and nothing else about the language would change.
> I'll have one more go changing the words slightly.
>
> When you extend an interface you MUST write at least
> one method.
>
> When you extend a class you MAY not have to write any
> methods although you probably will want to.
>
> Agreement?
Nope.
Only an interface can extend an interface, and in that case you don't implement any methods.
I assume you meant when you implement an interface though. But again, no: public interface Foo {}
public class Bar implements Foo {
// I don't have to write any methods
}
Really, there's no difference in the underlying meaning of the words. As UJ says, they could both just be replaced by, for example, "inherits." All they really do is emphasize or highlight what's already determined by the types of the two participants in the relationship.
> public class Bar implements Foo {
>// I don't have to write any methods
> } [/code]
Also: public interface Foo {
public void baz();
}
public abstract class Bar implements Foo {
// I don't have to write any methods
}
> In principle there is no difference. These two
> keywords could as well be replaced with one, say
> inherits.
In principle there's no difference between a class and an interface either. The interface keyword could be dropped. The compiler could recognise whether a class carries implementation or not and complain if you tried multiple inheritance of implementation.
So the distinction between extends and implements, and class and interface basically is just "syntactical sugar".
> Having just reviewed the posts above I think all the
> answers and comments have omitted an important point
> for those using the extend and implement key
> words...
>
> A class may implement many intefaces but may extend
> only one class.
While that's true, it's not really a significant difference between implements and extends.
As already mentioned, we could replace both with inherits, and then we'd just say that a class may inherit from many interfaces but only from one other class.
Besides which, you can list multiple types after "extends" even in current Java.
> > A class may implement many intefaces but may
> extend
> > only one class.
>
> While that's true, it's not really a significant
> difference between implements and extends.
>
> As already mentioned, we could replace both with
> inherits, and then we'd just say that a class
> may inherit from many interfaces but only from one
> other class.
>
> Besides which, you can list multiple types
> after "extends" even in current Java.
OK