• Post Reply Bookmark Topic Watch Topic
  • New Topic

Interfaces' methods' implementation  RSS feed

 
Ramender Mall
Ranch Hand
Posts: 311
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gud Morning to all f u(acc. to my timezone here),

I am really confused with this interface thing ...I have seen(at more than 1 places)...that we implement an interface and the definitions of the methods are no where to see...

e.g.



Cud u please tell me WHERE are the definitions of these methods used with "hc"...
I saw the source code of HttpConnection: and as expected, it does not have any definitions, just the declarations are there, as it ought to be in an interface..

Thanx n have a gud day...

Ramy
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ramender, please UseRealWords...

If you look at that piece of code, the method Connector.open returns an object of a class that implements interface HttpConnection. So the definition of the methods are in the class of that object.

Another example:

In this case, an ArrayList object is created, but it's assigned to a variable of type List. You can do this (without casting) because ArrayList implements interface List; an ArrayList is a List.

It's a good idea to program like this; you program to an interface, and not a specific implementation of an interface, so you decouple your code from the specific implementation.

Suppose in the case of the List above, someone discovers later on that it's better to use LinkedList (maybe for reasons of performance, for example). If you have programmed to the interface List, it is very easy to change to LinkedList; you only have to change one line of code:

If you would have written "ArrayList names = new ArrayList();", and you would have used ArrayList everywhere around your program, it would be very hard to change it all into LinkedList.
 
Ramender Mall
Ranch Hand
Posts: 311
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanx Jesper,

Sorry for that shortcut typing...

Now coming to the code...
the code snippet that I wrote there is of a J2ME Project( I posted it in this forum because, I thought this is basic concept)...
And "Connector" is a class given in API's ....but what it returns is an Interface "Connection" ...
Connection is extended by the HttpConnection....
Now as Connection is an interface, there is no defintion of any method there,(as a matter of fact, it has only one method and that is "close()")....

So, I am still not getting it--> where are the definitions??..

ramy...
 
Rajasekar Elango
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

It will be in your applications classpath..check the jars that you have included..

tools like JWhich , JarClassFinder (try google) will be helpful to find..

Regards,
Raja
 
Ramender Mall
Ranch Hand
Posts: 311
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Raja,

I think i am not able to get it across..
See in the beginning of my code i have included classes..



Putting my older snippet here--->



And javax.microedition.io.Connector.open is


Through various calls, (like here we are calling an overloaded form of open), we end up in javax.microedition.io.Connector.openPrim, which is-->


So, definitions of following methods
1. hc.setRequestMethod()
2. hc.openOutputStream()
3. uc.openPrim(name, mode, timeouts), because ConnectionBaseInterface is also an interface,


i know that i am missing something, but what is it ...

Are all these methods defined in clazz(i dont know what is the actual class its referring to)?....

all these classes and interfaces, except my older lines, are in J2ME api's...


thanx to all of you..


ramy...
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you asking a question on how interfaces work? Or are you asking where is the source code for all this stuff?

Henry
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

This piece of code dynamically loads a class called Protocol. The exact package name of that class is determined from various parts (classRoot, platform, protocol).

The class Protocol that it loads implements interface ConnectionBaseInterface. That interface contains a method openPrim, which is called, and the method returns an object of a class that implements interface Connection.

Try this in your source code to find out exactly which class is behind the Connection object:
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hc = (HttpConnection) Connector.open(url);


In this code, Connecter returns some implementation of HttpConnection. You are right it can be very frustrating trying to find out exactly what implementation so you can go look at it. Connector's right to never tell you what implementation it might return or why is part of the value of interfaces. Connector is allowed to keep secrets meaning you cannot write code that depends on its inner workings, keeping you safe from all kinds of evil.

I added a plugin to Eclipse called something about ShowImplementors that pops up a list of implementations of a highlighted interface or abstract class or method. Very nice if you happen to have the source for the implementations in the classpath.

Without something like that you may be reduced to printing HttpConnection.getClass().getName() to find out what's really going on, and be warned that it might not always be the same answer.

BTW: Connector and HttpConnection are not in the standard libraries are they? Where did they come from?
 
Ramender Mall
Ranch Hand
Posts: 311
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

Thanx a lot to all of you...Finally i found it


By Henry
Are you asking a question on how interfaces work? Or are you asking where is the source code for all this stuff?

Actually I knew where the source code is, but i wanted to know particularly the place where full definitions of the methods were given...




BY JESPER,

This piece of code dynamically loads a class called Protocol. The exact package name of that class is determined from various parts (classRoot, platform, protocol).

The class Protocol that it loads implements interface ConnectionBaseInterface. That interface contains a method openPrim, which is called, and the method returns an object of a class that implements interface Connection.

Try this in your source code to find out exactly which class is behind the Connection object:



Now this is what I was looking for, Thanks a lot jesper ..
Your-->System.out.println(hc.getClass().getName());
Returned--->com.sun.midp.io.j2me.http.Protocol
And BINGO....





By Stan,

BTW: Connector and HttpConnection are not in the standard libraries are they? Where did they come from?


Thanx Stan for the help,
and these are in standard libraries of J2ME



Thanx...
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!