This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line! See this thread for details.
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
Regards,<BR>Travis M. Gibson, SCJP<BR>Java Developer<BR>www.travismgibson.com<BR>travis@travismgibson.com
Ajith Kallambella
Sheriff
Joined: Mar 17, 2000
Posts: 5782
posted
0
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
Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
Stephanie Grasson
Ranch Hand
Joined: Jun 14, 2000
Posts: 347
posted
0
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
Joined: Aug 30, 2000
Posts: 3141
posted
0
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
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 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.
Make visible what, without you, might perhaps never have been seen. - Robert Bresson
mahesh deshpande
Greenhorn
Joined: Oct 26, 2000
Posts: 19
posted
0
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
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
Joined: Nov 03, 2000
Posts: 6
posted
0
Thanks for the excellent discussion on extends vs implements! It cleared up several items for me.
Anonymous
Ranch Hand
Joined: Nov 22, 2008
Posts: 18944
posted
0
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
Joined: Sep 03, 2008
Posts: 78
posted
0
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..
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
Isn't it funny how there's always time and money enough to do it WRONG?
Andrew Hodel
Greenhorn
Joined: Apr 04, 2013
Posts: 8
posted
0
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
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.)