• 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

Mock Blues 1

 
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can somebody please explain this question from Khalid Mughal Mock test :

Q5. Given that a static method doIt() in the class Work represents work to be done, which block of code will succeed in starting a new thread that will do the work?

a. Runnable r = new Runnable() {
public void run() {
Work.doIt();
}
};
Thread t = new Thread (r);
t.start();

b. Thread t = ne Thread () {
public void start() {
Work.doIt();
}
};
t.start();

c. Runnable r = new Runnable () {
public void run() {
Work.doIt();
}
};
r.start();

d. Thread t = new Thread (new Work());
t.start();

e. Runnable t = new Runnable () {
public void run() {
Work.doIt();
}
};
t.run();

Answer is a. Please explain how can an interface be instantiated like this and why other options are wrong.

Secondly on the MindQ test :
Q7. Represent the number 6 as a hexadecimal literal.
Answers are 0x6, 0x06, 0X6, 0X06
I wrote the answer as 0x0006. Is this wrong?
 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I modified A to be a complete program, here it is:


Notice the semicolon I point out. This should be a hint- you have an anonymous class. So it is creating an anonymous class with the interface runnable and overriding the public void run. You can not instantiate an interface- but you can create an anonymous inner class.

Anyway, onto the meat of the problem. In order to use a runnable, you have to attach it to a thread, override run, and then start the thread. The above example does all three, and as you can see if you run it, works.



Okay, here we create a thread, again as an anonymous class. However, they override the start method, not the run method. The start method, as I understand it, creates the new stack and initiates the run method with this new stack. Here they have overridden the start method, so no new thread will be created, although I believe it will actually call the doit, but in the same thread as the calling function.



This should not compile. There is no start method for a Runnable, so it won't know how to resolve the r.start(). There is no thread created either. A Runnable does not have anything to do with a thread except that provides an interface for a thread to invoke.



Here Work is being passed in to the thread. The thread is expecting a class of type Runnable, and no where has it been said that Work implements Runnable. IF it did, then the run() would be called on it, not doit(). But if it did implement public void run, was an implementer of Runnable, and the run method called doit() I think it would work. I just tried it and it works



Here we have an anonymous class implementing runnable, and no thread is created. The Work.doIt should be invoked, but not in a separate thread.


Question for the day:

If you create a class which does NOT implement Runnable, but does implement public void run(), can you cast it into a type Runnable and hand it to the thread constructor?

The answer is yes- you can compile it, but it throws a runtime exception- ClassCastException when it reaches the call.
[ September 19, 2004: Message edited by: Tom Tolman ]
 
JPraveen Kumar
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Tx Tom, I think the other four options can be dumped outright but am still unclear abt how the first option is correct. How do u say that its an anonymous class and not an anonymous interface. Let me generalize it by this code. Hope you can explain it in context of the question :

interface inter{}

public class cls {
public static void main(String args[])
{
inter i = new inter(){};
System.out.println (i.getClass()); //Prints class lib.cls$1
}
}

Now what is i? Is it an anonymous cls class which has an inner implementation of inter? Did the implementation come automatically because of the declaration of type inter? Why should this be allowed as anonymous classes cannot extend or implement and when the class cls by itself has nothing todo with the interface inter.

To confuse me further I changed inter to
interface inter{void k();}

The error is
<anonymous lib.cls$1> is not abstract and does not override abstract method k() in lib.inter

Can u please tell me wht is happening here?

Tx JPK
 
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by JPraveen Kumar:
Tx Tom, I think the other four options can be dumped outright but am still unclear abt how the first option is correct. How do u say that its an anonymous class and not an anonymous interface. Let me generalize it by this code. Hope you can explain it in context of the question :

interface inter{}

public class cls {
public static void main(String args[])
{
inter i = new inter(){};
System.out.println (i.getClass()); //Prints class lib.cls$1
}
}

Now what is i? Is it an anonymous cls class which has an inner implementation of inter? Did the implementation come automatically because of the declaration of type inter? Why should this be allowed as anonymous classes cannot extend or implement and when the class cls by itself has nothing todo with the interface inter.

To confuse me further I changed inter to
interface inter{void k();}

The error is
<anonymous lib.cls$1> is not abstract and does not override abstract method k() in lib.inter

Can u please tell me wht is happening here?

Tx JPK




Annonymouse classes CAN extend or implement(actually it has to, one time either not both)on the fly. Thats what annoynmouse class declarations do. In the case of implementing(you dont specify this keyword) an interface you have to implement the defined methods just like you would implement that interface in a normal class.

In the above case when you redefine inter with a method, your not declaring it in the annonymouse class declartion which you have to like so



inter i = new inter(){ void k() { } };


referance
http://www.witscale.com/scjp_studynotes/chapter7.html#chap7_4
http://java.sun.com/docs/books/tutorial/java/javaOO/innerclasses.html
 
reply
    Bookmark Topic Watch Topic
  • New Topic