• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Question: override (Static methods)

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One Dan's Mock Exam Question:
class C {
void printS1() {System.out.print("C.printS1 ");}
static void printS2() {System.out.print("C.printS2 ");}
}
class D extends C {
void printS1(){System.out.print("D.printS1 ");}
void printS2() {System.out.print("D.printS2 ");}
public static void main (String args[]) {
C c = new D();
c.printS1(); c.printS2();
}
}
What is the result of attempting to compile and run the above program?
a. Prints: C.printS1 C.printS2
b. Prints: C.printS1 D.printS2
c. Prints: D.printS1 C.printS2
d. Prints: D.printS1 D.printS2
e. Runtime error
f. Compiler error
g. None of the above
the answer is "f" compiler error: "printS2() in D cannot override printS2() in C;overridden method is static"
if I change "void printS2() {System.out.print("D.printS2 ");}" into "static void printS2() {System.out.print("D.printS2 ");}" in Class D , the file is compiled properly. I got this running results:
"D.printS1 C.printS2"
My question is :
1.If the printS2 method is overriden or it happen to have a same method in Class D.
2.Why I got this output but not D.printS1 D.printS2 or C.printS1 C.printS2?
Thanks in advance!
Sincerely Sam
[ January 03, 2003: Message edited by: sam huang ]
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is not legal to override a static method with a non-static method. Similarly, it is not allowed to hide a non-static method with a static method.
Please check out JLS 8.4.6 Inheritance, Overriding, and Hiding (and ff).
 
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sam,
The compile-time error is generated for the reasons described by Valentin.
If you declare D.printS2 as a static method, then D.printS2 hides C.printS2. In contrast, method printS1 is an instance method so D.printS1 overrides C.printS1. Method hiding is not the same as method overriding. Please use the link provided by Valentin to read about the differences.
An easy rule to remember is as follows. Static methods are invoked based on the type of the reference. Instance methods are invoked based on the type of instance.
The method invocation statements used in the program are as follows.
c.printS1(); c.printS2();
In both cases the reference, c, is of type C, but the instance is of type D because c is declared as follows.
C c = new D();
The method printS1 is an instance method so printS1 is invoked based on the type of the instance which is D. You have modified the declaration of method printS2 so that it is static in both classes C and D. Method printS2 is therefore invoked based on the type of the reference which is C.
 
sam huang
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got it.
Thanks a lot.
Sam
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

i have 3 questions:
1. can we conclude that "static and private methods can't be overridden, they're hidden instead"?
2. what about final methods? they're neither overridden nor hidden?
3. is statement "static methods are implicitly final" true? if so, does it contradict #2?
 
Ranch Hand
Posts: 366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1) static methods are hidden
2) non static methods(instance methods) are overridden
3) final methods cannot be overridden
I hope I am clear
Sri
 
reply
    Bookmark Topic Watch Topic
  • New Topic