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 extends vs implements Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "extends vs implements" Watch "extends vs implements" New topic

extends vs implements

Jolie Lee
Ranch Hand

Joined: May 08, 2005
Posts: 87
public class Point extends Object implements shape{}

what is the differents between implements and extends?

from what i know class Object is automatically imported into all classes, if that so, can i write

public class Point implements shape{}?

Jys<br /><a href="" target="_blank" rel="nofollow"></a><br /> <br />Trying my very best to learn java, please forgive me if i'm asking some really stupid questions.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
"extends" means the next token is the name of a class. "implements" means the next token is the name of an interface. If you search the forums or the FAQ you'll find we've had a lot of conversations about the merits "abstract class vs interface" and the design implications. In general I recommend the most abstract thing that will work. Sometimes that means interface (more abstract) and sometimes the problem really calls for extending an abstract class (less abstract). See if you can find the other discussions and let us know what further questions come to mind.

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Jolie Lee
Ranch Hand

Joined: May 08, 2005
Posts: 87
let's see...

extends is from a class where as implements is from interface.
extends - single inheritance where as implements - multiple inheritance.

class only inherit constants from an interface...
fred rosenberger
lowercase baba

Joined: Oct 02, 2003
Posts: 11923

class only inherit constants from an interface...

i don't think that's quite correct...

if b extends a, then b has EVERYTHING a has... constants, methods, member variables. if you write nothing more than

public class A extends B{};

then class A and B are identical. you can then put MORE stuff into B, and override parts of A, but everything that's in A is also in B.

implements - multiple inheritance.

again, not quite. the keyword "implements" really only means that the class you are writing will have all the methods defined in the interface. there is no garantee of HOW they will work, or what they will do exactly... just what parameters they take and what kind of object (if any) they return. Implementing something doesn't GIVE you anything - it says you promise to DO something in your class. like signing a contract.

inheriting (extending) from a class GIVES you the methods/variables defined in the parent class for free.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Edwin Dalorzo
Ranch Hand

Joined: Dec 31, 2004
Posts: 961
Let's clear that last point.

So it I do this

So, interface fields, as they are static are not actually inherited, but by convetion it can be accessed from a child class by their simple name as lons as there are not conflict with other interfaces.

It is different in classes, because the class fields are not obligatory public static and final.
[ May 14, 2005: Message edited by: Edwin Dalorzo ]
Edwin Dalorzo
Ranch Hand

Joined: Dec 31, 2004
Posts: 961
It is also very important to understan the extends and implents trade-offs.

If you extend a class, for exampe an abstract class, then you will not be able to extend other class, because java sticks to the convetion of single inheritance. The advantage is that, an abstract class or fully implemented class already have some funtionality implemented.

In the other hand, implementing an interface lets you extend other classes, but you have to implement them somehow every time that you use them.

That is the reason for which classes like Thread can use a Runnable interface instead of just extending the class Thread. Because once you extend Thread, you cannot extend other class. But if you use Runnable, you can extend other class and still keep the threading functionality,
[ May 14, 2005: Message edited by: Edwin Dalorzo ]
I agree. Here's the link:
subject: extends vs implements
It's not a secret anymore!