Home arrow static arrow Java Programming [Archive] - A Click & Hack Program Puzzler
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - A Click & Hack Program Puzzler
This topic has 14 replies on 1 page.

Posts:88
Registered: 98-01-06
A Click & Hack Program Puzzler  
Aug 6, 2004 1:00 PM



 
Given the following code:
// First Classpackage hack; import click.CodeTalk; public class TypeIt {     public static void main(String[] args) {        ClickIt clickit = new ClickIt();        clickit.doIt();    }     private static class ClickIt extends CodeTalk {        void printMessage() {            System.out.println("Hack");        }    }} // Second Classpackage click; public class CodeTalk {     public void doIt() {        printMessage();    }     void printMessage() {        System.out.println("Click");    }}
Running the First Class, what does it print?
1. Click
2. Cough
3. Hack
4. None of the above.
 

Posts:24,036
Registered: 2/3/03
Re: A Click & Hack Program Puzzler  
Aug 6, 2004 1:01 PM (reply 1 of 14)



 
You ought to be able to det the answer very easily by compiling and running the code.
 

Posts:14,142
Registered: 99-04-02
Re: A Click & Hack Program Puzzler  
Aug 6, 2004 1:06 PM (reply 2 of 14)



 
I want to say it prints "Hack"... but I also have this feeling there's some trick going on here...
 

Posts:31,095
Registered: 4/30/99
Re: A Click & Hack Program Puzzler  
Aug 6, 2004 1:10 PM (reply 3 of 14)



 
Note that the two classes are in different packages, and the printMessage() method has package-private visibility.
 

Posts:14,142
Registered: 99-04-02
Re: A Click & Hack Program Puzzler  
Aug 6, 2004 1:10 PM (reply 4 of 14)



 
and it prints "Click"... Probably that it's a package protected method overriden in another package, so the CodeTalk class can't really call the one in the ClickIt class. Something sounds fishy here, though... like at least a missing compiler warning or something.
 

Posts:31,095
Registered: 4/30/99
Re: A Click & Hack Program Puzzler  
Aug 6, 2004 1:15 PM (reply 5 of 14)



 
Nothing fishy at all. You don't get a compiler warning when your class has a method that has the same signature as a private method in its superclass, it just assumes you realize you aren't inheriting the method but just shadowing it. (Is that the right term?) Same thing here.
 

Posts:88
Registered: 98-01-06
Re: A Click & Hack Program Puzzler  
Aug 6, 2004 1:24 PM (reply 6 of 14)



 
With the class CodeTalk unchanged how about this:
package hack; import click.CodeTalk; import java.lang.reflect.Method; public class TypeIt {     public static void main(String[] args) throws Exception {        ClickIt clickit = new ClickIt();        clickit.doIt();        Class clickitclass = ClickIt.class;        clickit.printMessage();        Class[] parms = new Class[] {};        Method m = clickitclass.getMethod("printMessage", parms);        m.invoke(clickit, parms);    }     private static class ClickIt extends CodeTalk {        void printMessage() {            System.out.println("Hack");        }    }}
Running the above yields

1. Click Click Click
2. Click Hack Hack
3. Click Hack Click
4. None of the above
 

Posts:6,750
Registered: 1/25/04
Re: A Click & Hack Program Puzzler  
Aug 6, 2004 1:31 PM (reply 7 of 14)



 
I say 2 (but I haven't run it).
 

Posts:37,103
Registered: 3/30/99
Re: A Click & Hack Program Puzzler  
Aug 6, 2004 1:49 PM (reply 8 of 14)



 
@OP: Why don't you just run it and see?
 

Posts:14,142
Registered: 99-04-02
Re: A Click & Hack Program Puzzler  
Aug 6, 2004 1:52 PM (reply 9 of 14)



 
Nothing fishy at all. You don't get a compiler warning
when your class has a method that has the same
signature as a private method in its superclass, it
just assumes you realize you aren't inheriting the
method but just shadowing it. (Is that the right
term?) Same thing here.

Okay... that makes sense.
 

Posts:88
Registered: 98-01-06
Re: A Click & Hack Program Puzzler  
Aug 6, 2004 1:54 PM (reply 10 of 14)



 
@OP: Why don't you just run it and see?

You ought to be able to det the answer very easily by compiling and running the code.

I have run both and learned something new, and that's the point. Josh and Neal are coming out with a whole book devoted to problems like this.
 

Posts:37,103
Registered: 3/30/99
Re: A Click & Hack Program Puzzler  
Aug 6, 2004 1:58 PM (reply 11 of 14)



 
@OP: Why don't you just run it and see?

You ought to be able to det the answer very easily
by compiling and running the code.

I have run both and learned something new, and that's
the point. Josh and Neal are coming out with a whole
book devoted to problems like this.

So you weren't actually looking for help? You were just trying to enlighten others here?
 

Posts:88
Registered: 98-01-06
Re: A Click & Hack Program Puzzler  
Aug 6, 2004 2:02 PM (reply 12 of 14)



 
Yes.

For people who have attended JavaOne the title was a take on Bloch and Gafter's CodeTalk, "Program Puzzlers" they call themselves Click and Hack the Type It brothers.
 

Posts:6,750
Registered: 1/25/04
Re: A Click & Hack Program Puzzler  
Aug 6, 2004 2:04 PM (reply 13 of 14)



 
Hm, the intersection of NPR and JavaOne...
 

Posts:1,025
Registered: 04-03-17
Re: A Click & Hack Program Puzzler  
Aug 6, 2004 2:08 PM (reply 14 of 14)



 
Hm, the intersection of NPR and JavaOne...

I miss Sunday mornings at my old job (yes, I did say "Sunday"). A co-worker would come in with the latest puzzle heard on NPR. It was neat.
 
This topic has 14 replies on 1 page.