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


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "inheritance question" Watch "inheritance question" New topic
Author

inheritance question

naren babu
Greenhorn

Joined: Sep 20, 2003
Posts: 8
_________
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 hammer3 {
public static void main(String[] args) {
A a1 = new A(); B b1 = new B();
C c1 = new C(); A c2 = new C();
c2.m1(a1); c2.m1(b1); c2.m1(c1);
}}

_________________
Since c2 is of object type C I expect CCC
But the answer is AAA.
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
The method name of your example is overloaded. The methods are not overridden.
c2.m1(a1); c2.m1(b1); c2.m1(c1);

The compiler chooses class A, because the compile-time type of c2 is A.
The compiler chooses method A.m1(A a), because the types A, B, C of the arguments a1, b1, c1 can be converted to the parameter type A.
At run-time the virtual machine would override A.m1(A a) based on the actual type C of the object. However, there is no method C.m1(A a) in class C. So overriding does not occur in this example.
----
The method resolution process takes place at compile-time based on the declared types of the object reference and the declared types of the arguments. This process determines which [overloaded] form of a method should be invoked, but not which implementation of that method. At run-time the actual type of the object the method is invoked upon is used to find an implementation of the method that was determined at compile time. The Java Programming Language 6.9.1
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
Hi Naren.
Some people teach us that the type of the reference determines the class of an instance variable, whereas the actual type of the object determines the class of an instance method.
Then we work Dan Chisholm�s exams and we have to correct what we have learned. By working through all of his questions on overriding and overloading, you will learn what you need to know.
There are two phases to deciding which method will be executed. The first phase is what the compiler does. The compiler of course only knows compile time types.
The second phase is what the virtual machine does at run-time. When the code is executing, the virtual machine knows the actual type of the object. Before the virtual machine invokes the method selected by the compiler, the virtual machine might use an overriding method instead.
There is another, more intuitive and conceptual way of explaining what is going on, related to the fact that A is a polymorphic variable. But the JLS describes method invocation in terms of compile-time steps and run-time steps. I figure it�s safe to go along with the JLS.
Besides the mechanical steps of method invocation, the JLS offers this insight - Resolving a method name at compile time is more complicated than resolving a field name because of the possibility of method overloading. Invoking a method at run time is also more complicated than accessing a field because of the possibility of instance method overriding. (JLS 15.12)
[ October 08, 2003: Message edited by: Marlene Miller ]
naren babu
Greenhorn

Joined: Sep 20, 2003
Posts: 8
thanks for your answers
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
I am going to try to explain this conceptually (polymorphism) rather than mechanically (compile-time and run-time steps).
c2 is a polymorphic variable. The variable c2 is of type A. c2 can hold a value of class A or any subclass of A. An object of a subclass of A can be used anywhere an object of class A can be used.
c2.m1(x) is a method invocation with respect to an object referenced by c2. The method is invoked on an object of class A. If the object is a subclass of A, the object is being used as if it were an object of class A. Therefore, the method m1 must be a member of class A.
However at run-time, the method m1 of A can be overridden, based on the type of the object. In this example, the method m1 of A is not overridden.
[ October 08, 2003: Message edited by: Marlene Miller ]
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
Here is an example from the Whizlabs mock exams that shows even when overriding (not overloading) occurs, it is important to consider both the compile-time and run-time aspects of method invocation.

The method B.getStr() overrides A.getStr(). Notice that A.getStr() throws Exception, B.getStr() does not.
At compile-time, when the compiler resolves the method invocation obj.getStr(), the method A.getStr() is chosen. Since A.getStr() throws Exception, the method invocation obj.getStr() must handle the Exception.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: inheritance question