difference between implements and extends

Hi,i am new to java. can any one help me to understand the difference between implement and extend Thanks in advance
[137 byte] By [krish_javaa] at [2007-10-2 6:26:33]
# 1
implements is for inheriting interfaces, because your class has to implement them, extends is when your class inherits from another class.
CeciNEstPasUnProgrammeura at 2007-7-16 13:28:24 > top of Java-index,Java Essentials,New To Java...
# 2
Because you're extending the superclass' functionality.
CeciNEstPasUnProgrammeura at 2007-7-16 13:28:24 > top of Java-index,Java Essentials,New To Java...
# 3
http://java.sun.com/docs/books/tutorial/java/concepts/interface.html
dmbdmba at 2007-7-16 13:28:24 > top of Java-index,Java Essentials,New To Java...
# 4

> 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.

targaryena at 2007-7-16 13:28:24 > top of Java-index,Java Essentials,New To Java...
# 5

> 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.

Gargoylea at 2007-7-16 13:28:24 > top of Java-index,Java Essentials,New To Java...
# 6

> 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.

targaryena at 2007-7-16 13:28:24 > top of Java-index,Java Essentials,New To Java...
# 7
OK You are much more accurate.I over-did the simplifying didn't I?I tried to focus on extending a class with complete methods which might not be true.I just hope I helped a little.Maybe not.
Gargoylea at 2007-7-16 13:28:24 > top of Java-index,Java Essentials,New To Java...
# 8
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?
Gargoylea at 2007-7-16 13:28:24 > top of Java-index,Java Essentials,New To Java...
# 9
> i am new to java. can any one help me to> lp me to understand the difference between implement> and extend In principle there is no difference. These two keywords could as well be replaced with one, say inherits.
..u...j...a at 2007-7-16 13:28:24 > top of Java-index,Java Essentials,New To Java...
# 10
Did you spot it?I said extend an interface when I should have written implements!!Dooh!!
Gargoylea at 2007-7-16 13:28:24 > top of Java-index,Java Essentials,New To Java...
# 11

> 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.

targaryena at 2007-7-16 13:28:24 > top of Java-index,Java Essentials,New To Java...
# 12
> When you extend an interface you MUST write at least> one method.Serializable.
CeciNEstPasUnProgrammeura at 2007-7-16 13:28:24 > top of Java-index,Java Essentials,New To Java...
# 13

> 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

}

targaryena at 2007-7-16 13:28:24 > top of Java-index,Java Essentials,New To Java...
# 14

> 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".

..u...j...a at 2007-7-16 13:28:24 > top of Java-index,Java Essentials,New To Java...
# 15
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.
Gargoylea at 2007-7-20 18:56:31 > top of Java-index,Java Essentials,New To Java...
# 16

> 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.

targaryena at 2007-7-20 18:56:31 > top of Java-index,Java Essentials,New To Java...
# 17

> > 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

Gargoylea at 2007-7-20 18:56:31 > top of Java-index,Java Essentials,New To Java...