File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes implement,extend Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "implement,extend" Watch "implement,extend" New topic


Kalaiselvan Selvamani
Ranch Hand

Joined: Sep 10, 2001
Posts: 32
Hii Pals,
I want to know the exact difference between implement and extend, Please tell me with example.
Thankx in Advance
Mohanraj Naidu

Joined: Sep 10, 2001
Posts: 7
When we want to inherite members and methods from a class we use keyword "extend"
class Test2 extends Test1 { // where Test1 is a class

and while inheriting from interface we go for "implement" keyword
class Example2 implements Example1 { // where Example1 is an
{ // interface

Mohanraj Naidu
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Extends can also be used when a class is inheriting from other class.
e.g Subclass extends superclass {
where subclass inherited from Superclass
Class can also implement an interface.
class MyClass extend Yourclass implements Interface {
Any interface can also implements morethan one interfaces.
eg. interface A implements B, C {
Michael Fitzmaurice
Ranch Hand

Joined: Aug 22, 2001
Posts: 168
Further to the above answers, any class can IMPLEMENT multiple interfaces, whereas it can EXTEND only one class. However, an interface can EXTEND multiple interfaces (this does not constitute 'multiple inheritance' per se, since no functionality is actually inherited).
EXTENDS implies that you INHERIT certain properties and behaviour from a superclass, whereas IMPLEMENTS implies only that certain functionality is implemented directly within the implementing class. IMPLEMENTS is like a contract adhered to by the implementing class, where it agrees to provide the implementation of a predetermined set of methods.
In both cases, the extending/implementing class can be treated polymorphically as being of the type of the superclass/interface in question.
Hope this helps

"One good thing about music - when it hits, you feel no pain"
Bob Marley

"One good thing about music - when it hits, you feel no pain" <P>Bob Marley
Cindy Glass
"The Hood"

Joined: Sep 29, 2000
Posts: 8521
Please change your name to be compliant with JavaRanch's naming policy.
Your ID should be 2 separate names with more than 1 letter each. We really want this to be a professional forum and would prefer that you use your REAL name.

"JavaRanch, where the deer and the Certified play" - David O'Meara
I agree. Here's the link:
subject: implement,extend
It's not a secret anymore!