The Department of Computer Science & Engineering
cse@buffalo
STUART C. SHAPIRO: CSE 115 C

CSE 115
Introduction To Computer Science for Majors I
Lecture C
Lecture Notes #16
Stuart C. Shapiro
Spring, 2007


Polymorphism

If several versions of the method myMethod() have been defined, all with no parameters, the version that will be used to execute the statement
var.myMethod();
depends not on the declared type of var, but on the actual type of the object that is the value of var when the statement is executed.

Varargs

A method may take an arbitrary number of arguments if, in the header of the method definition, the type of the last parameter is followed by "..." as shown here:
accessControl ReturnType methodName(Type1 param1, TypeLast... paramLast)
In that case, the last parameter is bound to a collection of the values of the arguments following the required arguments. In the method body, they can be operated on within a for each loop. For example, the body of the above method might contain the loop
for (TypeLast var: paramLast) {
    var.someMethod();
}

First Previous Next

Copyright © 2007 by Stuart C. Shapiro. All rights reserved.

Last modified: Sat Oct 20 09:33:35 EDT 2007
Stuart C. Shapiro <shapiro@cse.buffalo.edu>