• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question from dan mock exam

 
suresh kamsa
Ranch Hand
Posts: 149
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1056
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In each case, the compiler picks the most specific method that matches the argument types.
 
suresh kamsa
Ranch Hand
Posts: 149
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Ron/others can be more specific on this
 
Barkat Mardhani
Ranch Hand
Posts: 787
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi There:
Can someone explain this method call:
m(a=null,b=null);
Thanks,
Barkat
 
Ron Newman
Ranch Hand
Posts: 1056
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 37
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But what about the first call to m(null, null)
 
zarina mohammad
Ranch Hand
Posts: 104
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 787
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 149
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 787
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 125
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Anthony Villanueva
Ranch Hand
Posts: 1055
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 125
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried. It compiled and printed successfully.
 
Anthony Villanueva
Ranch Hand
Posts: 1055
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1056
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1865
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic