• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

ghost method

 
Ranch Hand
Posts: 161
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I finally managed to get SortNames running
using an interface and its obligatory
method; I am wondering though, why the program
runs without me actively using (calling) this method;
it seems to be called automatically by implementing the interface??
Is there some rule or general explanation to this?
thanks for un-ghosting this,
Juliane
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maybe you have to look at whatever is doing the sorting for you? What are its formal parameters as listed in the API?
-Barry
 
whippersnapper
Posts: 1843
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by juliane gross:
I am wondering though, why the program
runs without me actively using (calling) this method; it seems to be called automatically by implementing the interface??


It's being called way, way, way behind the scenes by code that you've set in motion by calling a particular method on the Collections class.
(I think I sketched this process out once in an old post, but I can't seem to find it now.)
The fact that you have implemented the interface and passed the implementing object along to the process that needs it is what makes all this work.
Think for a second how powerful this is in this case. You haven't had to worry about coding complicated sorting algorithms or data structures. All you've had to do essentially is define a rule for the process to follow, then set it all in motion. Pretty cool, no?
Is there some rule or general explanation to this? thanks for un-ghosting this,
This might not exactly be the kind of rule or explanation you're looking for, but I think it might help:
Any time you're told in Java that in order to do something you want to do you should either override some particular method or implement some particular interface (which amounts to writing some method(s)), part of the trick is realizing that something somewhere will be calling that method. (Sometimes you may want to know the details of what's calling the particular method. Sometimes you may not care.)
 
Michael Matola
whippersnapper
Posts: 1843
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you *really* want to see where your method is getting called, take a look at lines 1237, 1249, and 1256 in the source for the java.util.Arrays class.
But don't say I didn't warn you!
The beauty of the exercise is that you don't have to know what's going on at these lines of code in order to use them to do want you want.
 
Michael Matola
whippersnapper
Posts: 1843
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Barry is, of course, excused from any reading of source code, since we know already that he doesn't like to.
 
juliane gross
Ranch Hand
Posts: 161
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks, Michael!
I will check out that source code ..
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I did my share of reading source code when debugging VMS Operating System device drivers! On microfiche and assembly language, or Bliss (Whatever happend to Bliss?)
AND, as Marilyn and Pauline will probably tell you, I only write source code now, I never read it
[ January 20, 2003: Message edited by: Barry Gaunt ]
 
Michael Matola
whippersnapper
Posts: 1843
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you go Java source code surfing, Juliane, it'll probably be easier to follow if you start from the methods you're using in the Collections class, rather than from the lines in Arrays that I pointed out.
 
juliane gross
Ranch Hand
Posts: 161
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Michael wrote:

If you *really* want to see where your method is getting called, take a look at lines 1237, 1249, and 1256 in the source for the java.util.Arrays class


I couldn't find a link to the source code at API, or Sun's homepage.
Where exactly can I read that source code?
It should be somewhere in the internet, or do
I have to get a book??
I am embarrassed by my ignorance, but hell, I never pretended NOT to be a Java greenhorn..
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You should have a file like src.zip somewhere in the load of stuff you extracted to get the j2sdk1.4.1 kit.
By the way it's got a big label on saying "Enter at your own risk!"
-Barry
[ January 24, 2003: Message edited by: Barry Gaunt ]
 
Michael Matola
whippersnapper
Posts: 1843
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think there's some disclaimer from Sun that they don't guarantee that the source code provided is what they actually compile into Java, but that it's some "typical" implementation.
Isn't it wild to see how much Java is written in Java itself.
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can unzip (or unjar) src.jar which is usually in the base (home) directory of your sdk (something similar to C:\jdk1.3.1 or C:\j2sdk1.4.1)
 
Marilyn de Queiroz
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Michael Matola:
I think there's some disclaimer from Sun that they don't guarantee that the source code provided is what they actually compile into Java, but that it's some "typical" implementation.


I've heard others say that, but I've never seen any disclaimer like that.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by juliane gross:

Is there some rule or general explanation to this?


You might want to do a google search on the "Strategy Design Pattern".
 
reply
    Bookmark Topic Watch Topic
  • New Topic