aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes The method main can be hidden Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "The method main can be hidden " Watch "The method main can be hidden " New topic
Author

The method main can be hidden

Devender Thareja
Ranch Hand

Joined: Jul 14, 2005
Posts: 187
Q23 from
http://www.valoxo.ch/jr/mocks/mock01a.html

Question 23. What happens when class Question23 is compiled and run? (Note: the code is in the file Question23.java). Select two correct answers.

A. Nothing is printed.
B. Prints: In Question23
C. Prints: In Question23Sub
D. Compilation error.
E. The method main can be hidden

Correct answer is A and E.

Nothing is printed because Class23's main method is empty. That means, it's not hidden by subclass's main method.
In what situation the method can be hidden?

Thanks in advance!


Devender Thareja
SCEA, SCBCD, SCJP
Nirav Patel
Greenhorn

Joined: May 27, 2005
Posts: 22
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!
shyam ramineni
Ranch Hand

Joined: Jul 08, 2005
Posts: 43
Hi,

Interesting thing to note is that when you compile Question23.java.
A new class file Question23Sub.class is also created in the same directory.

Try running
java Question23Stub and you get the following.
C:\shyam>java Question23Sub
In Question23

Bottom line Which class is marked public is not important.
Which class you are running is important.


Correct me if I am wrong

Thanks
Shyam Ramineni


<a href="http://hyderabaditeducation.com" target="_blank" rel="nofollow">http://hyderabaditeducation.com</a>
Devender Thareja
Ranch Hand

Joined: Jul 14, 2005
Posts: 187
Nirav,

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.


Shyam,

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.
Devender Thareja
Ranch Hand

Joined: Jul 14, 2005
Posts: 187
Can somebody help in clearing my doubt?

Thanks.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Actually hiding is a term with a specific meaning defined in JLS3 8.4.8.2. 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 ]

"I'm not back." - Bill Harding, Twister
Georgy Bolyuba
Ranch Hand

Joined: Feb 18, 2005
Posts: 162
Hi

Jim Yingst, it's yours 13000 th post! Congrats!

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


Can you explain it?


SCJP 1.4 (100%) Done.<br />SCJD (URLyBird 1.2.3 Started)
Satish Kumar
Greenhorn

Joined: Aug 08, 2005
Posts: 9
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 ?

rgds
satish


Regards<br />Satish
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
[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.
Georgy Bolyuba
Ranch Hand

Joined: Feb 18, 2005
Posts: 162
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...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: The method main can be hidden