• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Overloading static and private methods

 
author
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am studying for my SJCP Test, and I've come across a question in a mock exam that says "static and private methods cannot be overloaded." Is this true?
If so, please provide a URL for a reference.
Thanks,
Matt
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Matt ...
Are you sure it didn't say "static and private methods can't be overridden"? There is no problem with overloading a static or private method but they can't be overridden.
See the JLS §8.4.6 and §8.4.7
Hope that helps.
------------------

Jane Griscti
Sun Certified Java 2 Programmer
"When ideas fail, words come in very handy" -- Goethe
 
Matt Raible
author
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's the exact question - from gEs: Java 2.0 from www.mayaconsultancy.com.
Question: 35
Which of the following statements are true?
A. Static methods cannot be overriden
B. Static methods cannot be overloaded
C. Private methods cannot be overloaded
D. An overloaded method cannot throw exceptions not checked
in the base class
The correct answers on this test (according to the software) are b & c. This is wrong right? The correct answer should be a.
Also, on the question below, the answers are
A & C (according to the software) - is it right?
Question: 41
Which of the following statements are true?

A. static methods do not have access to the implicit variable
called this
B. a static method may not be overriden
C. a static method may not be overriden to be non-static
D. a static method may not be overloaded
Thanks,
Matt
[This message has been edited by Matt Raible (edited January 17, 2001).]
 
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Matt,
Q35. Correct ans is A.
Q41 Correct ans is A & C.

------------------
Regards,
Raj.
-------------------------
Afforts should be Appriciated.
-------------------------
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
I have had trouble here also.
Q41. Answer B is correct as well as A and C. Static methods cannot be overriden but they can be hidden. If you try to override a static method to non-static you get a compile error. If you try to override a static method to another static method you get no compile error but you are hiding the original method instead of overriding and therefore no delayed binding.
Hope that is clear.
 
Jane Griscti
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Matt,
Hopefully this code will make it a little clearer:

The above compiles cleanly and produces the following output:

As you can see static and private methods can be overloaded.
Scott is correct in saying static and private methods cannot be overridden; they can, however, be hidden. In the following code, I've created a class, Hidden, that extends StaticMethods and includes two methods with the same names as those in StaticMethods but with different parameters.

The code compiles cleanly and gives the following runtime output:

If overridding were allowed on static and private methods then this code would have produce a compile error. The rules state overridding methods must have the same signature (name and parameters) and return type as the method being overridden.
Therefore, the methods are not being overridden. If the methods did have exactly the same parameters they would still work; but not because of overridding; instead, they would hide the methods in the superclass.
Hope that helps.

------------------

Jane Griscti
Sun Certified Java 2 Programmer
"When ideas fail, words come in very handy" -- Goethe
 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jane,
I changed your Hidden class so that smethod and pmethod override the ones in StaticMethods class. The code compile and run. Is it right?
Thanks!
Zheng
public class Hidden extends StaticMethods {
static void smethod(int a) {
System.out.println("In Hidden.smethod(int a)");
}
private void pmethod( int a) {
System.out.println("In Hidden.pmethod(int )" );
}

public static void main(String[] args) {
Hidden.smethod(10);
new Hidden().pmethod(8);
}

}Hi
 
Jane Griscti
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes Zheng, it's right.
Just remember that technically, it's not overriding. Here are the excerpts from the JLS
JLS §4.6.2

If a class declares a static method, then the declaration of that method is said to hide any and all methods with the same
signature in the superclasses and superinterfaces of the class that would otherwise be accessible to code in the class. A
compile-time error occurs if a static method hides an instance method.

JLS §8.4.6.3

Note that a private method cannot be hidden or overridden in the technical sense of those terms. This means that a subclass
can declare a method with the same signature as a private method in one of its superclasses, and there is no requirement that
the return type or throws clause of such a method bear any relationship to those of the private method in the superclass.

Hope that helps.

------------------

Jane Griscti
Sun Certified Java 2 Programmer
"When ideas fail, words come in very handy" -- Goethe
 
Yeast devil! Back to the oven that baked you! And take this tiny ad too:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic