Win a copy of Spring Boot in Practice this week in the Spring 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

Marcus Green Test #3 Ques 17

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually this is a question which I have seen come up numerous times on this discussion group and I think a lot of people find it confusing:
Which of the following statements are true?

1) static methods do not have access to the implicit variable called this
2) a static method may not be overriden
3) a static method may not be overriden to be non-static
4) a static method may not be overloaded
Answer to Question 17)
Objective 1.2)
1) static methods do not have access to the implicit variable called this
3) a static may not be overriden to be non-static
The implicit variable this refers to the current instant of a class and thus and by its nature a static method cannot have access to it.
--------------------------------------------
However it is my belief and other people have supported this that in fact #2 is also correct. Although overriding a static method does not result in a compiler error, it does not actually override the base class method. It actually acts to shadow the base class method.
For example here is some test code:
class Inner {
static void hello() {
System.out.println ("Inner");
}
}
public class Test extends Inner {
int a = 0;

static void hello() {
System.out.println("Test");
}

public static void main(String[] args) {
Inner t = new Test();
t.hello();
}
}
Upon running the output is only: Inner
That shows that the base class method has in fact NOT been overridden! Please correct me if I am wrong!
 
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Eishan,
Good point. On the face of it it looks static methods are not overridden in base class. That is demonstrated by your example code.
But in addition to behavior, there are exceptions to be considered when overriding a method. A overriding method in subclass cannot throw more checked exceptions than method being overridden.
If you think hello() is not overridden in class Test, try declaring hello() to throw some checked exception. The compiler should flag an error.
This proves static methods with same signature and return type in base and derived classes are NOT unrelated . Hence, static methods can be overridden

[This message has been edited by Ramesh Donnipadu (edited September 29, 2000).]
 
Ramesh Donnipadu
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My apologies.
I checked up with JLS. Static methods are hidden. They are not overridden.
For more detailed information visit http://java.sun.com/docs/books/jls/html/8.doc.html#227928

[This message has been edited by Ramesh Donnipadu (edited September 29, 2000).]
 
reply
    Bookmark Topic Watch Topic
  • New Topic