File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Interface - not implemented Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Interface - not implemented" Watch "Interface - not implemented" New topic
Author

Interface - not implemented

Arnb Sen
Ranch Hand

Joined: Feb 23, 2004
Posts: 145
Is it correct that it is not necessary to implement interface and yet create an instance ?

Also, is it not compulsory that all methods in an interface must be an abstract ?

So how can the instance of an interface, call a method in the interface ?


Regards,<br />Arnab
Arnb Sen
Ranch Hand

Joined: Feb 23, 2004
Posts: 145
Isn't it correct that interface never implements a method ?
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

Interfaces never implement methods. Definition only.

(I do not understand your first post, sorry)


[My Blog]
All roads lead to JavaRanch
Arnb Sen
Ranch Hand

Joined: Feb 23, 2004
Posts: 145
so can I create an instance of the interface without implementing it ?
[ January 05, 2006: Message edited by: Arnb Sen ]
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Arnb Sen:
so can I create an instance of the interface without implementing it ?


No. Why do you ask?


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Sachin Dimble
Ranch Hand

Joined: Dec 07, 2005
Posts: 100
Arnab I think u can access the parameters in interface without implementing it.

Sachin.
Arnb Sen
Ranch Hand

Joined: Feb 23, 2004
Posts: 145
Because I am coming across in APIs, which contain interfaces, and sample codes which are creating instances of these interfaces without implementing them.

Also, methods are being called...

For example in J2ME,

you can create an instance of "Player" which is an interface.

Confused!
Arnb Sen
Ranch Hand

Joined: Feb 23, 2004
Posts: 145
access the parameters ? Can you please explain ?
Stuart Ash
Ranch Hand

Joined: Oct 07, 2005
Posts: 637
Originally posted by Sachin Dimble:
Arnab I think u can access the parameters in interface without implementing it.

Sachin.


I think he is referring to constants. But they are really outside the interface concept, so take it separately.


ASCII silly question, Get a silly ANSI.
Stuart Ash
Ranch Hand

Joined: Oct 07, 2005
Posts: 637
Please understand the difference between object instances and reference types. This thread might give you some picture.

Interface use works this way. The actual object would be of a type of implemented concrete class. But the reference can be of any super type of this concrete class. Possible supertypes being a. interface, b. abstract base class, c. concrete base class.

The classic example is that of Lists.

List list = new ArrayList();

Actual object type = ArrayList
Reference type = List

AbstractList list = new ArrayList();

Actual object type = ArrayList
Reference type = AbstractList

Hope this helps
Arnb Sen
Ranch Hand

Joined: Feb 23, 2004
Posts: 145
Player p = Manager.createPlayer("some parameters here");

This is a code in a class file which does not implement Player which is an interface. This is with reference to J2ME.

So how does this work ?
Stuart Ash
Ranch Hand

Joined: Oct 07, 2005
Posts: 637
Originally posted by Arnb Sen:
Player p = Manager.createPlayer("some parameters here");

This is a code in a class file which does not implement Player which is an interface. This is with reference to J2ME.

So how does this work ?


The class file in which you find this code NEED NOT implement anything.

I suspect there is a confusion between CLASS DEFINITION and CLASS USAGE here. Please ensure you have these things clear.

In the above case, the method returns an object VIA a reference of type Player (an interface perhaps). But the concrete object held by this refernce will be of a concrete implementing class - you can verify this by reading the implementation of the abvoe method.

Now, I can write another class, call it HonoluluDreams.java and use the above code, my class in no way implementing the Player interface.

Hope this is clear.
Arun Kumarr
Ranch Hand

Joined: May 16, 2005
Posts: 513

Something like this!!!



If you are not laughing at yourself, then you just didn't get the joke.
Arnb Sen
Ranch Hand

Joined: Feb 23, 2004
Posts: 145
No. did not understand completely.

So you are saying that...

I can create an instance of Player and that instance must contain a concrete object. So Manager.createPlayer() creates a concrete object ?
Arun Kumarr
Ranch Hand

Joined: May 16, 2005
Posts: 513

You can never instantiate ( create) an interface or an abstract class.

You can always have the reference type as an abstractclass/interface and have it point to the sub-class( which has implemented the interface or extended the abstract class).

Player p = something.getPlayer ();

Your getPlayer method would look something like this.

[ January 05, 2006: Message edited by: Arun Kumarr ]
Stuart Ash
Ranch Hand

Joined: Oct 07, 2005
Posts: 637
Originally posted by Arnb Sen:
No. did not understand completely.

So you are saying that...

I can create an instance of Player and that instance must contain a concrete object. So Manager.createPlayer() creates a concrete object ?


This concept will become clear if you understand the following:

reference
type
instance

These are explained, for example, by Bruce Eckel in this book. I had recommended this in a link pasted earlier in this thread.
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
This type of object creation goes on in many places. For example, in using JDBC, Statement and PreparedStatement are both interfaces, yet there are methods built in to Connection which create a concrete class that implements the interfaces and returns a reference to that object. In the statement you listed where a Player object is created, there is code that happens in the method you call which will implement the interface and return a reference to the object.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
So Manager.createPlayer() creates a concrete object ?


Yes. You know that the concrete class implements the Player interface (or extends the Player class). And that's all you need to know.

You don't know the exact class of that concrete object. Manager might have some logic like:

Your code which relies on the Player interface can work with either concrete class. Manager can introduce new concrete player types in the next release and you won't have to change your code at all. Cool, no?



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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Interface - not implemented