Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Interface - not implemented

 
Arnb Sen
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?
 
Arnb Sen
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Isn't it correct that interface never implements a method ?
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Interfaces never implement methods. Definition only.

(I do not understand your first post, sorry)
 
Arnb Sen
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so can I create an instance of the interface without implementing it ?
[ January 05, 2006: Message edited by: Arnb Sen ]
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Arnb Sen:
so can I create an instance of the interface without implementing it ?


No. Why do you ask?
 
Sachin Dimble
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arnab I think u can access the parameters in interface without implementing it.

Sachin.
 
Arnb Sen
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 145
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
access the parameters ? Can you please explain ?
 
Stuart Ash
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Stuart Ash
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 145
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 637
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 661
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Something like this!!!

 
Arnb Sen
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 661
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 637
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic