Hi Devender, Well, here in the code, the file is Question23.java and it contains public class Question23; so obviously the execution starts with 'that' class's main method, isn't it? Which is empty here!
Now if you make that subclass public and base class default and then execute; the main method of base class becomes hidden (shadowed!) by sub class main method. just try it!
It's overriding the static method by subclass static method. isn't it?
----<br />Nirav Patel<br />Software is the bottleneck in the digital world!
If you make base class default and subclass public, then the file name would also change accordingly (Question23sub.java). I would not consider it as hiding, because in this case Question23Sub is the main class.
That's a nice observation. I tried it and it works as you mentioned. But is that hiding of main method? It looks like you can execute both main depending on which class you want to run and none is hiding other.
Actually hiding is a term with a specific meaning defined in JLS3 220.127.116.11. That's the definition being used by the author of this mock exam question. I don't think you actually need to know the term for the real exam, but you do need to understand the behavior it represents.
[Nirav]: It's overriding the static method by subclass static method. isn't it?
No - it looks like overriding, but it's actually hiding. Static methods are never overridden; instead they are hidden. The difference is that overriding implies polymorphism - the JVM will use the actual instance of the class at run time to determine which version of hte method to invoke. With static methods, there is no instance (or if you supply one, the compiler will basically ignore it) and the compiler will make a call based on the compile-time class which is used to invoke the method. For a static method, the decision is completely handled at compile time, and no dynamic overriding will occur at run time. [ August 07, 2005: Message edited by: Jim Yingst ]
You said:[Jim]No - it looks like overriding, but it's actually hiding.
Ok. Let's check this code:
See it? Method foo() in class A is final. So what? We just hiding it, not overriding. I mean we cannot override static method, right? So we don't need to use final modifier. And it seems to have no effect.
But, my compiler says: Error: line (19) foo() in test.B cannot override foo() in test.A; overridden method is static final
hi I feel its possible to override a static method, Incase u want to stop that we need to make the method final.
am i correct ?
Joined: Jan 30, 2000
[George Bulyuba]: But, my compiler says: Error: line (19) foo() in test.B cannot override foo() in test.A; overridden method is static final
Yes, this is a longstanding mismatch between the compiler error message and the JLS. The people who originally wrote the compier message apparently have a somewhat more "flexible" (i.e. undefined) idea of what "override" means than do the people who wrote the JLS. If would be nice, certainly, if these two disparate groups could be brought together to form some sort of coherent whole. That hasn't happened so far, so my (hopefully temporary, where "temporary" means "for the last ten years at least, and who knows how much longer?") solution is this: since the people who wrote the JLS have publicly explained their thinking in exhaustive (and perhaps tedious) detail, while the people who wrote the error message have not explained themselves at all: simply ignore the error message. It's an error, sure, but the stated explanation is... well... wrong. Consider the JLS to be the only definitive source of information here. If necessary I could possibly invent explanations of what the authors of the JDK compiler really meant, but let's face facts: they haven't explained themselves publicly. Meanwhile, the people who wrote the JLs have. It may require several careful readings, but generally the JLS is remarkably self-consistent, and represents the best single authoritative source on most questions about the Java language.
[Satish]: I feel its possible to override a static method
That's nice, but it's not actually true. It's possible to hide a stic method (which is very similar to overriding), but it's not possible to override a static method.
[Satish]: Incase u want to stop that we need to make the method final.
Making a method final prevents both overriding (of nonstatic methods) and hiding (of static methods). The two effects are similar in many ways, but not the same.
Joined: Feb 18, 2005
Originally posted by Jim Yingst: It may require several careful readings, but generally the JLS is remarkably self-consistent, and represents the best single authoritative source on most questions about the Java language.
I've started thinking of updating my singnature...