1

What is the ordering of method invocation when you have an abstract class with method behaviour implemented and also when no behaviour is implemented?

Lets say my abstract class is called Abs and it has two subclasses, Sub1 and Sub2

In case 1, Abs contains the implementation code for method Meth1

public abstract class Abs{

  public void Meth1(){
    //Some code
  }
}

In a completely different class i have the method:

MyMethod(Abs a){
  a.Meth1();
}

where I pass either Sub1 or Sub2 as substitute for Abs

In case 2, Abs doesnt contain the implementation code (but Sub1 and Sub2 do)

public abstract class Abs{

  public abstract void Meth1();
}

and i call the same:

MyMethod(Abs a){
  a.Meth1();
}

after passing in either Sub1 or Sub2.

What is the ordering of the method calls in each case? Does it always go to the superclass Abs and then to the subclass? Does it go to the subclass first, because the sublclass was passed in as the parameter, then the JVM checks whether there is implementation code provided in the subclass and if not, then the superclass method is called if there is implementation code?

Mat
  • 202,337
  • 40
  • 393
  • 406
James T
  • 125
  • 1
  • 8
  • There's no such concept as *orderding of method calls*. Each instantiated object has all of its methods precisely defined (since an abstract object cannot be instantiated, and a non-abstract object cannot contain abstract methods). When you call a method, the JVM knows where to go directly, there's no need to "check" where to go. Note that I have no idea of the behaviour of the new `invokedynamic` bytecode, but a Java compiler will not emit it anyway. – Bruno Reis Aug 20 '11 at 14:16
  • Thanks, so on a sequence diagram there would only be reference to either the subclass or the superclass, not both? – James T Aug 20 '11 at 14:22
  • @James T: in a sequence diagram, you have a reference to an object, not a class. This object has a type, which is always a concrete type. An abstract class can't be instantiated. If a method is overridden by the concrete subclass, then this method and only this method is called. If not, its superclass method is called. That's the whole idea behind polymorphism and inheritance. – JB Nizet Aug 20 '11 at 14:48

2 Answers2

3

If the subclass overrides a method, then the parent method implementation is not called at all, unless the subclass has a super.overridenMethodName() call. It can be at the beginning or the end or anywhere else of the overriden method implementation.

On the other hand, if an abstract class has an abstract method, then subclasses are forced at the compile time to implement it and provide some logic.

Ilya Saunkin
  • 18,934
  • 9
  • 36
  • 50
  • Thanks, so on a sequence diagram there would only be reference to either the subclass or the superclass method call? – James T Aug 20 '11 at 14:22
  • @Elijah: the super method call can in fact be anywhere inside any method of the subclass, as many times as wanted. Not just at the beginning or at the end of the overridden method. – JB Nizet Aug 20 '11 at 14:38
  • Actually, i think only to subclass. That's the part of inheritance thing: that methods, unless overriden, are treated as subclass-own. – Ilya Saunkin Aug 20 '11 at 14:40
0

For a non-static method m, the method invocation expression o.m() is executed by determining the runtime class of the object referred to by o. Then, that class' method implementation (inherited or defined within the class itself) is invoked.

Every class inherits every (visible) method from its super class, unless it declares a method with the same signature, in which case the declared method implementation overrides the inherited one instead.

Put differently, the implementation in the most specific super class in invoked. That is all; in particular, inherited implementations of that method are not automatically invoked. The invoked method may, of course, in turn invoke any inherited method implementation using super.m().

Note that the rules are different for the invocation of static methods. Also, while the runtime class of the receiver (o in our example) determines the method to be invoked, overloading is resolved using the compile time types of the parameters, not their runtime types.

meriton
  • 68,356
  • 14
  • 108
  • 175