Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Extends vs. Implement?

 
Travis Gibson
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey fellow ranchers,
I'm just trying to determine what the real differences are between these two (Extend and Implement).
I know Extend is used to subclasse and Implement is used to inherit.
But what are some other differences. By the way can Abstracts and Interfaces have constructors?
Thanks,
Travis

 
Ajith Kallambella
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you want to subclass from an existing class( inheritance ) you use the extends keyword. When you want to implent an interface( remember interfaces are different than classes ), you use the implements keyword. That's the difference.
Hope that helps.
PS : I am moving this thread to Java in General(beginner)
forum.
Ajith
 
Stephanie Grasson
Ranch Hand
Posts: 347
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Travis,
Here's more about the difference between extends and implements:
The implements keyword allows you to have "multiple inheritance". The extends keyword does not.
For example:

The base class is Person. Mother and Wife are classes which extend base class Person. Wives and mothers are specific types of people.
But what if you have a Person who is both a wife and a mother?

The code above is illegal ... it will not compile. But you can accomplish such a task with interfaces (using the implements keyword). For example:

Hope this helps.
Stephanie
 
Jane Griscti
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Travis,
An Abstract class is the same as a regular class except it has one or more methods that are not implemented ie they are declared but have no executable code; so yes, they can have constructors.
An Interface can only have variables which are constants. Methods can be declared BUT can have no executable code. All interface methods are implicitly abstract.
An Interface does not have a constructor; as you cannot create an instance of an Interface. They guarantee behaviour, not how the behaviour occurs.
Hope that helps.
------------------
Jane
 
Travis Gibson
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for all your help.
Here's what I've come up with for our discussion:
1.) Both abstract classes and interfaces can be implemented and extended.
2.) interfaces can only contain variable which have an assigned value or prototype methods. Can be instantiated
3.) abstracts can contain anything a standard class can in addition to methods with no implementation. Cannot instantiated.
4.) A class which uses an interface must implement all methods from interface.
5.) A class doesn't have to implement all methods from abstract just those that require implementation.
FYI, did you know that an abstract method cannot also be static.
Anyway I'm just looking to fill in some weak spots before I take the SCJP on Nov. 19.

I hope to report back with much success,
Travis
 
Michael Ernest
High Plains Drifter
Sheriff
Posts: 7292
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a list of "yes but" answers to your summary:
1. abstract classes can be extended only. Interaces can be extended but only by other interfaces.
2. intefaces may only contain static values and cannot be instantiated, but they can be used as reference types in an instantiation, e.g.:
Runnable runner = new ActionThread();
4. An implementing class must take care of all methods in the interface, but that doesn't mean the implemented method has to be meaningful:
public class GUIWatcher implements ActionListener
{
public void actionPerformed(ActionEvent ae) {}
}
5. If you extend an abstract class but don't implement its "prototype" methods, your subclass will be considered abstract, and the compiler will (should) force you to put that in the class declaration.
Good luck on your preparation! These are good points to reivew.
Originally posted by Travis Gibson:
1.) Both abstract classes and interfaces can be implemented and extended.
2.) interfaces can only contain variable which have an assigned value or prototype methods. Can be instantiated
3.) abstracts can contain anything a standard class can in addition to methods with no implementation. Cannot instantiated.
4.) A class which uses an interface must implement all methods from interface.
5.) A class doesn't have to implement all methods from abstract just those that require implementation.

 
mahesh deshpande
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just want to go ahead with the same discussion with one more query i.e. why is it that all the methods in the Interface needs to be implemented
mahesh
 
Michael Ernest
High Plains Drifter
Sheriff
Posts: 7292
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Mahesh -
The purpose of an interface is to guarantee the behavior of any type that implements it. Let's say I have a class Widget that implements an interface called ActionListener. Widget declares it can do whatever an ActionListener is supposed to do. ActionListener defines actionPerformed(), so I guarantee that I have that method available and it can be called at runtime.
This way we can examine a reference at compile-time and determine if its type matches its intended use.
If we allowed partial implementation of an interface, we could not determine at compile-time which methods were being used and which ones weren't; therefore we would not be able to guarantee that any type implementing an interface actually matches its use.
This still does not mean the implementation has to be meaningful:

This code implements "the entire interface," but so what? Nothing comes of it. But that's a runtime issue. In compile-time, we're only concerned here that type-safety can be confirmed.
 
J. Daniel
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the excellent discussion on extends vs implements! It cleared up several items for me.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is something worth reading over once or twice. You can catch a lot of intermediate/advanced users touting interfaces as an alternative to multiple class inheritence, which is just not so.
This is not in contradiction to what Stepanie said, which was "The implements keyword allows you to have "multiple inheritance". The extends keyword does not." Since she was refering to the actual keywords and not a superclass vs interface.
From sun's tutorial:
Often interfaces are touted as an alternative to multiple class inheritance. While interfaces may solve similar problems, interface and multiple class inheritance are quite different animals, in particular:
-A class inherits only constants from an interface.
-A class cannot inherit method implementations from an
interface.
-The interface hierarchy is independent of the class hierarchy.
-Classes that implement the same interface may or may not be
related through the class hierarchy. This is not true for
multiple inheritance.
Yet, Java does allow multiple interface inheritance. That is,
an interface can have multiple superinterfaces.
 
geet kaur
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi..


I am still not clear with the difference.extend means we the class is inheriting evrythng that its superclass has except for some conditions. But what about implements??is it also related to inheritance..can someone please explain it..
 
Winston Gutkowski
Bartender
Pie
Posts: 10527
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
geet kaur wrote:I am still not clear with the difference.

Well I'm not sure that reviving a 12-year old thread is the way to go about it, but now that you have...

extend means we the class is inheriting evrythng that its superclass has except for some conditions...

Specifically, everything that isn't private or package-protected (if the subclass is in a different package).

But what about implements??is it also related to inheritance..can someone please explain it..

Implements is used with interfaces, so a class that implements an interface is a subtype of that interface. A class that extends another is a subclass. The main difference is that subclasses inherit code and fields, not just behaviour.

However, for more information, you should probably check the tutorials.

Winston
 
Andrew Hodel
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:

Specifically, everything that isn't private or package-protected (if the subclass is in a different package).



I thought that if you implement/extend something you get the package-protected? i guess what does it mean to be in a different package?... a different source file?
I know this is just extending an old thread but this has always caused me trouble and query.
Andrew
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Andrew Hodel wrote:
Winston Gutkowski wrote:

Specifically, everything that isn't private or package-protected (if the subclass is in a different package).



I thought that if you implement/extend something you get the package-protected?


Nope.

i guess what does it mean to be in a different package?... a different source file?


No. It's a combination namespace and access control facility, and it's directory-based, not file-based. (Although it's true that two classes in different packages will be in different source files. The converse doesn't hold though.)

Google for something like java package tutorial for more details.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic