• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Threads

 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
//1 public class Foo implements Runnable{

//2public void run (Thread t) {
System.out.println("Running.");
}
public static void main (String[] args) {
new Thread (new Foo()).start();
}
}
When compiling this programme where comilation error occurs?
I think that comilation error occurs at line 2 because the run method prototype is different.
Please tell me the answer
 
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes you are correct, Here are the details:
Runnable method has only one method with signature :
public void run();

So any class that implements the interface has to implement the method.When i say implement the method, the signature has to be the same, Actually there are various rules of implementation that has to be followed.
In your example you "The argument list has to me excatly same" rule is not followed, You are actually creating a new method run(Thread t) that takes t as argument and this does not override the run() of Runnable.
One of the correct implemetantions would be



Thanks
Deepak
 
sukhavasi vasavi
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here my question is why the compilation error shows at line 1 instead of at line2.Please explain
 
Deepak Jain
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The error is in line one because Test class does not implement the run() of the Runannable interface.

"The type Test must implement the inherited abstract method Runnable.run()"

Line # 2 does not cause any errors because it simply defines a new method run(Thread t) that takes t as argument and this does not override the run() of Runnable.

Hope this clears.
Any class that implements an interface must override all the methods declared in the interface or it must be declared as abstarct.
Thanks
Deepak
 
Does this tiny ad smell okay to you?
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic