• 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
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

overriding main method??

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


Hi Friends:
Is this source code correct... I think it should give error saying that "main is already defined" and it is giving this error when I ran it.But I am not getting how this is the correct answer in K&B mock??


Regards
Swapnil
"I apologize for writing the incorrect code and bothering u all...I have corrected this now"

[ July 05, 2006: Message edited by: Swapnil Trivedi ]
[ July 05, 2006: Message edited by: Swapnil Trivedi ]
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The other main method has a different signature.
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Hi,
You are implementing the main() method in the interface and returning a String on a void return type ..... ?


Finner

[ July 05, 2006: Message edited by: Finner Jones ]
[ July 05, 2006: Message edited by: Finner Jones ]
 
Swapnil Trivedi
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am really very sorry for my earlier post I wonder how I wrote that code Now I corrected it...it's like this:


Apologies !!!
Swapnil

[ July 05, 2006: Message edited by: Swapnil Trivedi ]

[ July 05, 2006: Message edited by: Swapnil Trivedi ]
[ July 05, 2006: Message edited by: Swapnil Trivedi ]
 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Swapnil that code will not compile for a number of reasons, including:

* Your interface has implementation, it should not.
* Your 'void' main method in class B is returning a String.

Plus there might be other compilation problems, but I've only had a quick look.

If you were to fix these problems you still wouldn't get a compile error for the reason you state since your 'main' method in your interface has no arguments - the static 'main()' method takes a String array.


Edit - you've edited your code so not everything I wrote is still true.
[ July 05, 2006: Message edited by: Andy Morris ]
 
Finner Jones
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Apologies accepted




The ammended code will have compile error "Duplicate method main in A".


I think it should give error saying that "main is already defined"


You are correct


But I am not getting how this is the correct answer in K&B mock??


I don't understand what you are asking here ?


Finner
 
Swapnil Trivedi
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But I am not getting how this is the correct answer in K&B mock??


This Question is from K&B master exam 2. In that, this answer is correct. They have asked that the class that implements this interface need to override which of the following methods.....(there were 4 methods in the option)
And I didn't choose this main(String[] wow) method option due to the same reason...But my answer was wrong. According to correct answer given, they are considering this main(String[] wow) method as correct option.


Regards
Swapnil
And Thanks for accepting my apologies Finner . I was feeling very bad for writing such a rubbbish code......

[ July 05, 2006: Message edited by: Swapnil Trivedi ]

[ July 05, 2006: Message edited by: Swapnil Trivedi ]
[ July 05, 2006: Message edited by: Swapnil Trivedi ]
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Swapnil,
There are three concepts, main() method, overriding rules and interface implemnation rules.
main() method facts:
You are free to have as many methods named main() as you like (or not at all), if no methods match the main() method the JVM looks for, then you won't be able to run the class by invoking Java using that class' name. You can still instanstiate the class from other code.

Overriding rules:
Now the code that you provided won't work due to overriding rules. You have to change the signature of the method to override the method, so if you implement interface A, you can't have a static void main(String[[] args) method(and you don't have to).

That should answer why k&B exam had option main(String[] args) selected. If you implement interface A you have to implement single method provided in that interface and if you do that you can't have similar method with only difference of having static as access modifier (regardless of method name is main or not).
I hope this helps.
 
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you post your question along with all options?

Thanks

Naseem
 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic