The Artima Developer Community
Sponsored Link

Java Answers Forum
Question of the Day

4 replies on 1 page. Most recent reply: Aug 8, 2002 3:09 PM by Thomas SMETS

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 4 replies on 1 page
Dan Chisholm

Posts: 11
Nickname: dchisholm
Registered: Jul, 2002

Question of the Day Posted: Jul 31, 2002 4:43 PM
Reply to this message Reply
Advertisement
class P {
static void printS1(){System.out.print("P.printS1 ");}
void printS2() {System.out.print("P.printS2 ");}
void printS1S2(){printS1();printS2();}
}

class Q extends P {
static void printS1(){System.out.print("Q.printS1 ");}
void printS2(){System.out.print("Q.printS2 ");}
public static void main(String[] args) {
new Q().printS1S2();
}
}


What is the result of attempting to compile and run the above program?

a. Prints: P.printS1 P.printS2
b. Prints: P.printS1 Q.printS2
c. Prints: Q.printS1 P.printS2
d. Prints: Q.printS1 Q.printS2
e. Runtime Exception
f. Compiler Error
g. None of the Above





Please see the next post for the answer.


Thomas SMETS

Posts: 307
Nickname: tsmets
Registered: Apr, 2002

Re: Question of the Day Posted: Jul 31, 2002 5:01 PM
Reply to this message Reply
class P 
{
  static void printS1() // 2
  {
    System.out.print("P.printS1 ");
  }
  void printS2() 
  {
    System.out.print("P.printS2 ");
  }
 
  void printS1S2 ()  //  1
  {
    printS1();
    printS2();
  }
}
 
class Q 
  extends P 
{
  static void printS1() // 2'
  {
    System.out.print("Q.printS1 ");
  }
 
  void printS2 ()
  {
    System.out.print("Q.printS2 ");
  }
 
  public static void main(String[] args) 
  {
    new Q().printS1S2();
  }
}

Suppose the code compiles, the thing is rather trivial.
_ Invoking S1S2 on Q must invoke the method of the lowest layer (see 1);
_ S1S2 invoke S1 (it's possible because S1 is defined on P), but 24 is invoked in fact (lowest level).
_ Similar reasonning applies to S2. As we invoke the method from an instance of Q, there is no problem (neither a static overload by a member method).

IMHO the answer is therefore : "d",
Prints: Q.printS1 Q.printS2

thomas,

Dan Chisholm

Posts: 11
Nickname: dchisholm
Registered: Jul, 2002

Re: Question of the Day Posted: Jul 31, 2002 5:01 PM
Reply to this message Reply
class P {
static void printS1(){System.out.print("P.printS1 ");}
void printS2() {System.out.print("P.printS2 ");}
void printS1S2(){printS1();printS2();}
}

class Q extends P {
static void printS1(){System.out.print("Q.printS1 ");}
void printS2(){System.out.print("Q.printS2 ");}
public static void main(String[] args) {
new Q().printS1S2();
}
}

Prints: P.printS1 Q.printS2

Static method Q.printS1 hides the static method P.printS1 in the super class P. Instance method Q.printS2 overrides the instance method P.printS2. Due the differences between the hiding of static methods and the overridding of instance methods the invocation of the two methods in P.printS1S2 produces different results. The method invocation expression printS1 results in the invocation of the hidden super class method P.printS1. The method invocation expression printS2 results in the invocation of the overridding sub class method Q.printS2.

Dan Chisholm

Posts: 11
Nickname: dchisholm
Registered: Jul, 2002

Re: Question of the Day Posted: Jul 31, 2002 5:09 PM
Reply to this message Reply
Thomas,

Your answer is indeed logical, however, the architects of the Java programming language made a different design choice. While an instance method of a subclass may override an instance method of a superclass that has the same signature, a static subclass method will instead hide a superclass method that has the same signature.

Thank you for your response!

Dan

Thomas SMETS

Posts: 307
Nickname: tsmets
Registered: Apr, 2002

Re: Question of the Day Posted: Aug 8, 2002 3:09 PM
Reply to this message Reply
Dead right indeed !
I didn't checked the Java Specs but ...

Thomas,

Flat View: This topic has 4 replies on 1 page
Topic: Invoking exe from Java Previous Topic   Next Topic Topic: spread of computer viruses - a java simulation

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use