This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

The method main can be hidden

 
Devender Thareja
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Nirav Patel
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
shyam ramineni
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Devender Thareja
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can somebody help in clearing my doubt?

Thanks.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Georgy Bolyuba
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Satish Kumar
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[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
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic