This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Five Lines of Code and have Christian Clausen on-line!
See this thread for details.
Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Overloading static methods

 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which of the following statements are true?
1) Static methods cannot be overriden
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
Marcus, you have given the answers to be 2) 3).
But i think the answer could only be 3) because look at the code below
class statics {
protected static void ameth() {
System.out.println("base");
}}
public class statics1 extends statics {
public static void ameth(int i) {
System.out.println("derived");
}
public static void main(String[] args) {

}
}
Any suggestions???
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Marcus isn't here, and I'm not sure where (or if) he gave the answer you cite. But my answer is that only statement 1 is correct - static methods cannot be overridden. You may think you're overriding one, but what you're really doing is hiding it. Check out the JLS here for an explanation. All the other statements are false. Specifically for statement 3, private methods can be overloaded:
<code><pre>class Test
private void print() {
System.out.println("No argument");
}
private void print(int i) {
System.out.println("Integer argument " + i);
}
public static void main(String[] args) {
Test t = new Test();
t.print();
t.print(1);
}
}
</pre></code>
 
Desperado
Posts: 3226
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which of the following statements are true?
From what I understand of the questions,
1) Static methods cannot be overridden: TRUE. They are hidden instead. (Is that the word?)

2) Static methods cannot be overloaded. FALSE. You create a new method from scratch every time you overload. The new method just happens to have the same name as another.

3) Private methods cannot be overloaded. FALSE. You can, in the same class. They don't exist as far as subclasses are concerned so... "you do the math".

4) An overloaded method cannot throw exceptions not checked in the base class. FALSE. SEE (2). An overloaded method is a new method.
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In R&H it's given that static methods cannnot be overridden to non-static methods. Does that mean you can over-ride it by a static method.
Prasad
 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, you can.
When a static method is overridden by a static method then this is called hiding.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    Bookmark Topic Watch Topic
  • New Topic