• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

inheritance question

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
_________
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.
 
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for your answers
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic