Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

output of this code..

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • 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: 17346
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
  • 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: 17346
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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: 17346
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
  • 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
 
There are 10 kinds of people in this world. Those that understand binary get this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic