aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question from dan mock exam Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Question from dan mock exam" Watch "Question from dan mock exam" New topic
Author

Question from dan mock exam

suresh kamsa
Ranch Hand

Joined: Jul 30, 2001
Posts: 149
class A {}
class B extends A {}
class C extends B {
static void m(A x, A y) {System.out.print("AA");}
static void m(A x, B y) {System.out.print("AB");}
static void m(B x, A y) {System.out.print("BA");}
static void m(B x, B y) {System.out.print("BB");}
public static void main(String[] args) {
A a;
B b;
m(null,null);
m(a=null,b=null);
m(b, a);
}
}
prints output as BBABBA but how I don't know. Can anyone explain, thanks
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
In each case, the compiler picks the most specific method that matches the argument types.


Ron Newman - SCJP 1.2 (100%, 7 August 2002)
suresh kamsa
Ranch Hand

Joined: Jul 30, 2001
Posts: 149
Hey Ron/others can be more specific on this
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
Hi There:
Can someone explain this method call:
m(a=null,b=null);
Thanks,
Barkat
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
The assignments of null are irrelevant; the compiler will pick the same method as if you had written
m(a,b);
Since a is declared to be an A and b is a B,
it picks this one:
static void m(A x, B y) {System.out.print("AB");}
Remember that since the methods are static only the declared (compile-time) type matters, not the actual (run-time) type.
[ August 20, 2002: Message edited by: Ron Newman ]
Larry Lecomte
Ranch Hand

Joined: Jun 14, 2002
Posts: 37
But what about the first call to m(null, null)
zarina mohammad
Ranch Hand

Joined: Jun 26, 2002
Posts: 104
m(null, null)
calls the more specific method m(B x,B y) of subclass B rather than m(A x,A y) of the superclass A
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
Hi Larry:
As Ron said, it will pick most specific method.
a and b are both pointing to null. So a call
m(null,null) will invoke m(b,b) because that is with most specific (derived) arguments.
Thanks
Barkat
suresh kamsa
Ranch Hand

Joined: Jul 30, 2001
Posts: 149
when
A a;
B b;
m(null, null);
Here the most specific method is m(b,b) because m(null,null) is followed by B b? Please clarify?
Thanks
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
Hi Suresh:
Your quote:

when
A a;
B b;
m(null, null);
Here the most specific method is m(b,b) because m(null,null) is followed by B b? Please clarify?
Thanks

That is not the case. The most specific refers
to most derived. See class hierarchy. m(B x, B y)
is the method that has most derived arguments
than any other method with same name.
Hope this helps
Barkat
[ August 20, 2002: Message edited by: Barkat Mardhani ]
Yan Bai
Ranch Hand

Joined: Jul 21, 2002
Posts: 125
Originally posted by suresh kamsa:
[QB]class A {}
...
public static void main(String[] args) {
A a;
B b;
...
}
}

I'm wondering why this could be compiled since a and b are local variables in main function. Isn't that true that local variables need to be initialized when they are declared? Or is this rule only applicable for primitives? Please help me out


SCJP 1.4
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055
Originally posted by Yan Bai:

I'm wondering why this could be compiled since a and b are local variables in main function. Isn't that true that local variables need to be initialized when they are declared? Or is this rule only applicable for primitives? Please help me out

Good point. References a and b should have been initialized.
Yan Bai
Ranch Hand

Joined: Jul 21, 2002
Posts: 125
I tried. It compiled and printed successfully.
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055
The compiler will only object if you attempt to use an uninitialized local variable. Still, it's a good habit to initialize local variables at once.
[ August 20, 2002: Message edited by: Anthony Villanueva ]
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
Local variables must be initialized before they are used. The program you posted does not violate this rule. The statement
m(a=null,b=null);
initializes both a and b.
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
Anthony, Ron, Zarina,
Thank you all for answering the questions that were posted here. Although the remarks associated with the question don't say anything beyond what the three of you have already said I'll go ahead and post the remark anyway.

The first method call, m(null,null), calls method m with two null literals that have type null. Both null literals are promoted to type B because B is more specific than type A. As a result, m(B x, B y) is invoked. The second method call, m(a=null,b=null), calls m with a first parameter of type A and a second parameter of type B. As a result, m(A x, B y) is invoked because the parameter types are matched exactly. The third method call invokes m(B x, A y) because the parameter types match exactly.


Dan Chisholm<br />SCJP 1.4<br /> <br /><a href="http://www.danchisholm.net/" target="_blank" rel="nofollow">Try my mock exam.</a>
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question from dan mock exam