aspose file tools*
The moose likes Mock Exam Errata and the fly likes Marcus Green test1 Q16 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Mock Exam Errata
Bookmark "Marcus Green test1 Q16" Watch "Marcus Green test1 Q16" New topic
Author

Marcus Green test1 Q16

austin dsouza
Greenhorn

Joined: Dec 07, 2000
Posts: 5
Which of the following statements are true?
1) Methods cannot be overriden to be more private
2) Static methods cannot be overloaded
3) Private methods cannot be overloaded
4) An overloaded method cannot throw exceptions not checked in the base class
In the answer to this question author says Static method canot be overriden but overloaded.As per my knowledge static methods can be overriden as static but not as non static & non static method canot be overriden as static. Can you please clarify?
bill bozeman
Ranch Hand

Joined: Jun 30, 2000
Posts: 1070
****I edited this message based on something I just read about*****
A static method cannot be overridden, Marcus is correct. It appears as if you are overriding the method if you keep it static because the compiler doesn't complain and it does run, but you are actually hiding the method. The difference between hiding and overriding can be seen in an example from the JLS.

In this example you will get Goodnight, Dick. This is an example of method hiding. Since s is really a SuperHide, it will call SuperHide's method greeting() since it is figured out at compile time. However, when you call name(), a non-static method, this will be determined at runtime, so you get SubHide's implementation.

So, a static method cannot be overriden, it can only be hidden.
Sorry for the earlier confusion, but I am glad you put this out there because it now makes a lot more sense to me.
Thanks,
Bill
[This message has been edited by bill bozeman (edited December 08, 2000).]
austin dsouza
Greenhorn

Joined: Dec 07, 2000
Posts: 5
Bozeman i am not talking about answer to the question that i have mentioned , in the answer part of the question Marcus green mentioned that static methods canot be overriden,i think this statement is wrong & correct answer for this quetion is only 1 & not 2,3
bill bozeman
Ranch Hand

Joined: Jun 30, 2000
Posts: 1070
austin, I changed my message above because of something I just went out and found. Sorry for the mistake, but I hope the above makes sense. Let me know if you have questions.
Bill
vishad patel
Greenhorn

Joined: Dec 05, 2000
Posts: 17
right answer of qustion is only 4 .
see below given coding.1. public class TestFrame extends Frame {
2. public TestFrame(String title) {
3. super(title);
4. enableEvents(AWTEvent.WINDOW_EVENT_MASK);
5. }
6.
7. public void
processWindowEvent(WindowEvent we) {
8. System.out.println("Window event processed");
9. }
10. }
compile and run the programme .which will solve your problem.
austin dsouza
Greenhorn

Joined: Dec 07, 2000
Posts: 5
Bozeman Thank u for your reply. Still My doubt is not clear .I have provided the code below which will gives the output:-"overriden non-static method in Q3".can you please explain me why it works so & code that you have provided works differently.
Does in the code below ,the method has been not overriden?.Does static methods other than "public static void main(String args[])" will be executed when the class is loaded(i.e. there is no need to call static method from the "main" method)?
class Test
{
static void show()
{
System.out.println("non-static method in Test");
}
}
public class Q3 extends Test
{
static void show()
{
System.out.println("Overridden non-static method in Q3" );
}

public static void main(String[] args)
{
Test a = new Q3();
show();
}
}
austin dsouza
Greenhorn

Joined: Dec 07, 2000
Posts: 5
Vishad patel actually my problem is wether Static method can be overriden or not .you have mentioned answer 4 is correct.as per my knowledge Exceptions have nothing to do with overloading , similarly access modifiers & return type of the method ,while overloading method name must be same & arguments must be different or in different order. if i am wrong please correct me.
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
Hi Austin,
Static methods cannot be overridden, but they can be hidden. The following code, from the Jargon Mock Exam, is a good illustration of what happens.

When you compile and run this example, it outputs

You'd expect to see Child.test(), Child.test() since the actual type of anObj is Child (you can remove the cast and still see the same result), and dynamic binding causes the actual vs declared type to be used at Runtime.
However, because the method <code>test()</code> is <code>static</code>, Java doesn't bother to search for a matching method in the actual type; it simply uses the declared type, Base, as the rules say static methods cannot be overridden.
Hope that helps.
------------------
Jane
The cure for boredom is curiosity.
There is no cure for curiosity.
-- Dorothy Parker


Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
Leon Webster
Greenhorn

Joined: Dec 06, 2000
Posts: 10
Here's what the Java Language Specification has to say about this:
"If a class declares a static method, then the declaration of that method is said to *hide* [emphasis in the original text] any and all methods with the same signiture in the superclasses and super interfaces of the class that would otherwise be accessable to the code in the class. A compile-time error occurs if a static method hides an instance method. In this respect, hiding of fields, for it is permissible for a static variable to hide an instance variable.
"A hidden method can be accessed by using a qualified name or by using a method invocation expression that contains the key word super or a cast to the superclass type. In this respect hiding of methods is similar to hiding of fields."
I know more now than I did before...
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Marcus Green test1 Q16