aspose file tools*
The moose likes Beginning Java and the fly likes keyword Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "keyword" Watch "keyword" New topic
Author

keyword

suresh rajaraman
Greenhorn

Joined: Aug 21, 2010
Posts: 3
can anyone please tell the difference between

1. extend and implement

2. class implement an interface
interface implements interface

Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2861
    
  11

Well, a class implements an interface because it provides code to implement the methods defined by the interface. A class extends another class because it inherits the implementation of the its superclass, though of course it has the option of modifying the implementation if it chooses to.

An interface can only extend another interface, not implement it? Why? Probably because it doesn't actually implement anything, so extends is a better choice as a keyword.
Soumyajit Hazra
Ranch Hand

Joined: Jun 26, 2007
Posts: 136
Always keep in mind the simple rule. Same type extends but different type implements. So class A want to inherit class B you have to use extends as both are of same type class. If B is interface then different type so implements. If both A and B interface then again same type so extends need to be used.


Java Programmer | SCJP 1.5 | SCWCD 1.4
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

The Java tutorial is a good place to start learning about Java.


[My Blog]
All roads lead to JavaRanch
Kr Manish
Ranch Hand

Joined: Jul 30, 2010
Posts: 138
Soumyajit Hazra wrote:Always keep in mind the simple rule. Same type extends but different type implements. So class A want to inherit class B you have to use extends as both are of same type class. If B is interface then different type so implements. If both A and B interface then again same type so extends need to be used.

This approach to "remember" is not good for a beginner, or for anyone for that matter ! We should be able to actually understand the meaning of the words than to just "learn by heart" when to use which one.

He should know that "extend" means carrying forward the code that was there originally in the "class" + adding more if required.
And that, "implementing" means providing my own implementation for implemented "interface", since there was not anything to begin with in the interface.


You know what I am saying ?
Soumyajit Hazra
Ranch Hand

Joined: Jun 26, 2007
Posts: 136
He should know that "extend" means carrying forward the code that was there originally in the "class" + adding more if required.
And that, "implementing" means providing my own implementation for implemented "interface", since there was not anything to begin with in the interface.

This concept is already described in Greg's post. Assuming that Suresh reading the post from top to bottom I posted my view.
This approach to "remember" is not good for a beginner, or for anyone for that matter !
Depends on individual. If someone have a real desire to learn thing he/she will keep on asking for reason instead of just seeing a line and remembering it. So the moment I say those words the question arise why? And then comes the explanation part.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39885
    
  28
[Pedantic mode]You said "keyword" (which is hardly a good thread title), and you said extend and implement. The keywords are extends and implements. That difference may appear trivial, but just try to get it past the compiler [/Pedantic mode]
 
Consider Paul's rocket mass heater.
 
subject: keyword