File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Dan's mock exam- OOP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Dan Watch "Dan New topic
Author

Dan's mock exam- OOP

Priya Venkatesan
Ranch Hand

Joined: Nov 02, 2002
Posts: 61
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

Joined: Mar 07, 2003
Posts: 330
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.


SCJP 1.4<br />SCWCD 1.4
Reshma Shanbhag
Ranch Hand

Joined: Sep 17, 2002
Posts: 202
Hi Priya,
This question has been discussed in detail in past at
Overriding/Overloading
Hope this helps you
reshma


SCJP 1.4, SCWCD 1.4
Reshma Shanbhag
Ranch Hand

Joined: Sep 17, 2002
Posts: 202
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Dan's mock exam- OOP