• 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

output of this code..

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
please bare with me if this question is repeated because i am not sure of the correct choice.This question is from valiveru's mock test

question 4:
what happens when you compile this code:


The options are:

A: doesnt compile as you cannot override static methods
B:compiles and fails at runtime
c: compiles and prints "Hi pal!!I am a baseclass"
D:compiles and prints "Hi pal!! I am a subclass"

I marked as A as the answer but the answer is C!! can you override static method. If Both the classes were in the same file is it possible then to overirde the static method..
[ October 11, 2005: Message edited by: Mark Spritzler ]
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Preetha.

Wow, three duplicate posts. How did you manage that. Well I deleted the other two duplicate of this post. Please try not to post the same question twice.

Mark
 
Mark Spritzler
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First the code you typed won't compile.

Here is the code with minor changes to compile. Although it doesn't follow the Sun Java Coding Standard.



Second, the correct answer would be "D" because the instance type is of subclass, so it will call the subclasses static method and print out "Hi pal!! I am a subclass:

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

can you override static method.

No. Static methods are not inherited; they can be hidden, but not overridden.
 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
The above code works even if i change the modifier for of static method sayhello to public, then how is the static method hidden.Can you explain please.

Regards
Abhijit.
 
Mark Spritzler
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Static methods are specific to a Class. Therefore it is only valid for that Class itself, it does not belong to super or sub classes. just itself. It does not even belong to one instance.

So a subclass with the same named and signature static method is completely different than the other. It is as if they really have different names and signatures.

Mark
 
Do not threaten THIS beaver! Not even with this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic