انت هنا الان : شبكة جامعة بابل > موقع الكلية > نظام التعليم الالكتروني > مشاهدة المحاضرة

Overloading Methods

الكلية كلية تكنولوجيا المعلومات     القسم قسم شبكات المعلومات     المرحلة 3
أستاذ المادة مهدي عبادي مانع الموسوي       10/11/2013 20:33:26
1. Conversions and Overloading

The determination of type correctness is actually not as simple as described previously, for two reasons.

First. Java allows certain implicit conversations of a value of one type to a value of another type.
Implicit conversations involve only the primitive types. For example, Java allows chars to be widened to the numeric types. Thus, the assignment to n in the following is legal:


char c=’a’;
int n=c;

Second, conversations involve computation, that is, they cause a production of a new type (of the variable types) that is then assigned to the variable.

After the compiler determines the conversation needed to make the assignment legal, it generates the code needed to produce the new value.

2. Overloading
In addition, Java allows overloading. This means that there can be several methods with the same name.

For example, consider a class C with the following methods:
Public class c {
static int comp( int i, long j) // def1.
static float comp( long i, int j) // def 2
static int comp (long i, long j) // def3
}

This class provides three overloaded definitions of comp. when there are overloaded definitions, several of them might work for a particular call. For example, suppose you have the declarations:

int x;
long y;
float z;

In Java, an int (32 bits ) can be widened to a long (64 bits), and also a float (32 bits ) can be widened to a long (64 bits), Therefore a call C.com(x, y) could go to either the first definition of comp (since here the types match exactly) or the third definition of comp (by widening x to a long). The second definition is not possible since it isn’t possible to widen a long to an int.

The rule used to determine which method to call when there are several choices, as in this example, is “most specific”. A method m1 is more specific than another method m2 if any legal call of m1 would also be a legal call of m2 if more conversations were done.

For example, the first definition of comp would be selected for the call C.com(x,y) since it is more specific than the third definition.


Note:
If there is no most specific method, a compile time error occurs for example, all three definitions matches for the call C.comp(x,x). however, none of these is most specific and therefore the call is illegal .
The programmer can solve the ambiguity in case like this by making the conversation explicitly, for example C.comp ((long) x,x) select the second definition.


المادة المعروضة اعلاه هي مدخل الى المحاضرة المرفوعة بواسطة استاذ(ة) المادة . وقد تبدو لك غير متكاملة . حيث يضع استاذ المادة في بعض الاحيان فقط الجزء الاول من المحاضرة من اجل الاطلاع على ما ستقوم بتحميله لاحقا . في نظام التعليم الالكتروني نوفر هذه الخدمة لكي نبقيك على اطلاع حول محتوى الملف الذي ستقوم بتحميله .