Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Dan's mock exam- OOP

 
Priya Venkatesan
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Iam not able to understand why the result comes out this way,
class A {
void m1(A a) {System.out.print("A");}
}
class B extends A {
void m1(B b) {System.out.print("B");}
}
class C extends B {
void m1(C c) {System.out.print("C");}
}
class D {
public static void main(String[] args) {
A c1 = new C();
A c2 = new C();
A c3 = new C();
C c4 = new C();
c1.m1(c4);
c2.m1(c4);
c3.m1(c4);
}
}

The result is "AAA";
My understanding is that, although we are assigning new C() to A, the underlying object is C, so the method m1 of C should be called, giving the result "CCC". Should'nt?
I tried removing the parameters(c4) from all the method calls (also from methods). Now the result is "CCC", the same as what I expected. Can someone explain why it happens this way.
 
dennis zined
Ranch Hand
Posts: 330
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My understanding is that, although we are assigning new C() to A, the underlying object is C, so the method m1 of C should be called, giving the result "CCC". Should'nt?

This is true for overridding. The example shown uses overloading.
 
Reshma Shanbhag
Ranch Hand
Posts: 204
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Priya,
This question has been discussed in detail in past at
Overriding/Overloading
Hope this helps you
reshma
 
Reshma Shanbhag
Ranch Hand
Posts: 204
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
adding to what others have mentioned in previous posts .......
The question posted above is regarding overloading and not overriding.
In overloading the signature of the method declaration differs from one another. Methods can be Overloaded in the class it is defined, or in the subclass of its class. In case of overriding, methods are overridden on signature-by-signature basis. Methods are overridden in subclasses of the class and resolution of the method call is determined at the runtime.

Invocation of overloaded methods are determined at the compile time, depending on the type of the reference(not the type of the object contained at runtime), type and the order of the arguments. The compiler chooses the most specific form of
the method.
For E.G.

O/P for this example is ----> A .

At //Line 1 the invocation is to method 'method' with reference of type A and with argument of kind 'b'. The compiler looks for method named 'method' in class A, and selects the most appropriate of the methods available with the same name.
In the above example mentioned 'b' can be up-casted to both 'Object' as well as 'A'. But as any argument that be passed to method(A a) can be passed to method(Object o), method(A a) is more specific and will be invoked.
Hope this helps
Reshma
[ March 13, 2003: Message edited by: Reshma Pai ]
[ March 13, 2003: Message edited by: Reshma Pai ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic