Supervisor Ebtsam AbdelHakam [email protected] Department of Computer Science Najran University...
-
Upload
herbert-pearson -
Category
Documents
-
view
213 -
download
0
Transcript of Supervisor Ebtsam AbdelHakam [email protected] Department of Computer Science Najran University...
![Page 1: Supervisor Ebtsam AbdelHakam ebtsamabd@gmail.com Department of Computer Science Najran University 24/2/2014 Ebtsam Abdelhakam 1.](https://reader035.fdocuments.us/reader035/viewer/2022070412/5697bf7d1a28abf838c8473f/html5/thumbnails/1.jpg)
Object Oriented ProgrammingLecture (4.1)
SupervisorEbtsam AbdelHakam
[email protected] of Computer
ScienceNajran University
24/2
/201
4
Ebtsam Abdelhakam
1
![Page 2: Supervisor Ebtsam AbdelHakam ebtsamabd@gmail.com Department of Computer Science Najran University 24/2/2014 Ebtsam Abdelhakam 1.](https://reader035.fdocuments.us/reader035/viewer/2022070412/5697bf7d1a28abf838c8473f/html5/thumbnails/2.jpg)
Outline» Method overloading» Method overloading benefits» Constructor overloading» Using objects as parameter
24/2
/201
4
Ebtsam Abdelhakam
2
![Page 3: Supervisor Ebtsam AbdelHakam ebtsamabd@gmail.com Department of Computer Science Najran University 24/2/2014 Ebtsam Abdelhakam 1.](https://reader035.fdocuments.us/reader035/viewer/2022070412/5697bf7d1a28abf838c8473f/html5/thumbnails/3.jpg)
3
Ebtsam Abdelhakam
Method Overloading
● Method overloading is to define two or more methods within the same class that share the same name, as long as their parameter declarations are different.
● Method overloading is one of the ways that Java supports polymorphism.
● When an overloaded method is invoked, Java uses the type and/or number of arguments as its guide to determine which version of the overloaded method to actually call.
● Thus, overloaded methods must differ in the type and/or number of their parameters.
● While overloaded methods may have different return types, the return type alone is insufficient to distinguish two versions of a method.
● When Java encounters a call to an overloaded method, it simply executes the version of the method whose parameters match the arguments used in the call.
24/2
/201
4
![Page 4: Supervisor Ebtsam AbdelHakam ebtsamabd@gmail.com Department of Computer Science Najran University 24/2/2014 Ebtsam Abdelhakam 1.](https://reader035.fdocuments.us/reader035/viewer/2022070412/5697bf7d1a28abf838c8473f/html5/thumbnails/4.jpg)
4
Ebtsam Abdelhakam
Overloading method example24
/2/2
014
![Page 5: Supervisor Ebtsam AbdelHakam ebtsamabd@gmail.com Department of Computer Science Najran University 24/2/2014 Ebtsam Abdelhakam 1.](https://reader035.fdocuments.us/reader035/viewer/2022070412/5697bf7d1a28abf838c8473f/html5/thumbnails/5.jpg)
5
Ebtsam Abdelhakam
24/2
/201
4
![Page 6: Supervisor Ebtsam AbdelHakam ebtsamabd@gmail.com Department of Computer Science Najran University 24/2/2014 Ebtsam Abdelhakam 1.](https://reader035.fdocuments.us/reader035/viewer/2022070412/5697bf7d1a28abf838c8473f/html5/thumbnails/6.jpg)
6
Ebtsam Abdelhakam
Overloading example analysis● The value of overloading is that it allows related
methods to be accessed by use of a common name.● As you can see, test( ) is overloaded four times. ● The first version takes no parameters, the second
takes one integer parameter, the third takes two integer parameters, and the fourth takes one double parameter.
● The fact that the fourth version of test( ) also returns a value is of no consequence relative to overloading, since return types do not play a role in overload resolution.
24/2
/201
4
![Page 7: Supervisor Ebtsam AbdelHakam ebtsamabd@gmail.com Department of Computer Science Najran University 24/2/2014 Ebtsam Abdelhakam 1.](https://reader035.fdocuments.us/reader035/viewer/2022070412/5697bf7d1a28abf838c8473f/html5/thumbnails/7.jpg)
7
Ebtsam Abdelhakam
Method overloading benefits
● Method overloading is an applying to polymorphism concept.
● The value of overloading is that it allows related methods to be accessed by use of a common name.
● It is left to the compiler to choose the right specific version for a particular circumstance.
● For example, you can use the name sqrt to create methods that return the square of an integer and the square root of a floating-point value.
24/2
/201
4
![Page 8: Supervisor Ebtsam AbdelHakam ebtsamabd@gmail.com Department of Computer Science Najran University 24/2/2014 Ebtsam Abdelhakam 1.](https://reader035.fdocuments.us/reader035/viewer/2022070412/5697bf7d1a28abf838c8473f/html5/thumbnails/8.jpg)
24/2
/201
4
Ebtsam Abdelhakam
8
![Page 9: Supervisor Ebtsam AbdelHakam ebtsamabd@gmail.com Department of Computer Science Najran University 24/2/2014 Ebtsam Abdelhakam 1.](https://reader035.fdocuments.us/reader035/viewer/2022070412/5697bf7d1a28abf838c8473f/html5/thumbnails/9.jpg)
9
Ebtsam Abdelhakam
Analysis
● As you can see, the Box( , , ) constructor requires three parameters.
● This means that all declarations of Box objects must pass three arguments to the Box() constructor.
● For example, the following statement is currently invalid:
● Box ob = new Box();● Since Box() requires three arguments, it’s an error to
call it without them.● Note that default constructor Box() can't be called,
when you define your constructor.
24/2
/201
4
![Page 10: Supervisor Ebtsam AbdelHakam ebtsamabd@gmail.com Department of Computer Science Najran University 24/2/2014 Ebtsam Abdelhakam 1.](https://reader035.fdocuments.us/reader035/viewer/2022070412/5697bf7d1a28abf838c8473f/html5/thumbnails/10.jpg)
10
Ebtsam Abdelhakam
Constructor Overloading
● In addition to overloading normal methods, you can also overload constructor methods.
● Constructor overloading: Same constructor declared with different parameters in the same class.
● Compiler differentiates which constructor is to be called depending upon the number of parameters and their sequence of data types.
24/2
/201
4
![Page 11: Supervisor Ebtsam AbdelHakam ebtsamabd@gmail.com Department of Computer Science Najran University 24/2/2014 Ebtsam Abdelhakam 1.](https://reader035.fdocuments.us/reader035/viewer/2022070412/5697bf7d1a28abf838c8473f/html5/thumbnails/11.jpg)
11
Ebtsam Abdelhakam
24/2
/201
4
![Page 12: Supervisor Ebtsam AbdelHakam ebtsamabd@gmail.com Department of Computer Science Najran University 24/2/2014 Ebtsam Abdelhakam 1.](https://reader035.fdocuments.us/reader035/viewer/2022070412/5697bf7d1a28abf838c8473f/html5/thumbnails/12.jpg)
12
Ebtsam Abdelhakam
24/2
/201
4
![Page 13: Supervisor Ebtsam AbdelHakam ebtsamabd@gmail.com Department of Computer Science Najran University 24/2/2014 Ebtsam Abdelhakam 1.](https://reader035.fdocuments.us/reader035/viewer/2022070412/5697bf7d1a28abf838c8473f/html5/thumbnails/13.jpg)
13
Ebtsam Abdelhakam
Using Objects as Parameters
24/2
/201
4
![Page 14: Supervisor Ebtsam AbdelHakam ebtsamabd@gmail.com Department of Computer Science Najran University 24/2/2014 Ebtsam Abdelhakam 1.](https://reader035.fdocuments.us/reader035/viewer/2022070412/5697bf7d1a28abf838c8473f/html5/thumbnails/14.jpg)
14
Ebtsam Abdelhakam
Using objects as parameters 24
/2/2
014
![Page 15: Supervisor Ebtsam AbdelHakam ebtsamabd@gmail.com Department of Computer Science Najran University 24/2/2014 Ebtsam Abdelhakam 1.](https://reader035.fdocuments.us/reader035/viewer/2022070412/5697bf7d1a28abf838c8473f/html5/thumbnails/15.jpg)
15
Ebtsam Abdelhakam
Using object as constructor parameter
● One of the most common uses of object parameters involves constructors.
● Frequently, you will want to construct a new object so that it is initially the same as some existing object.
● To do this, you must define a constructor that takes an object of its class as a parameter.
24/2
/201
4
![Page 16: Supervisor Ebtsam AbdelHakam ebtsamabd@gmail.com Department of Computer Science Najran University 24/2/2014 Ebtsam Abdelhakam 1.](https://reader035.fdocuments.us/reader035/viewer/2022070412/5697bf7d1a28abf838c8473f/html5/thumbnails/16.jpg)
16
Ebtsam Abdelhakam
24/2
/201
4
![Page 17: Supervisor Ebtsam AbdelHakam ebtsamabd@gmail.com Department of Computer Science Najran University 24/2/2014 Ebtsam Abdelhakam 1.](https://reader035.fdocuments.us/reader035/viewer/2022070412/5697bf7d1a28abf838c8473f/html5/thumbnails/17.jpg)
17
Ebtsam Abdelhakam
24/2
/201
4
![Page 18: Supervisor Ebtsam AbdelHakam ebtsamabd@gmail.com Department of Computer Science Najran University 24/2/2014 Ebtsam Abdelhakam 1.](https://reader035.fdocuments.us/reader035/viewer/2022070412/5697bf7d1a28abf838c8473f/html5/thumbnails/18.jpg)
this() with Constructors
» Suppose by accessing one constructor, the programmer may require the functionality of other constructors also but by creating one object only.
» "this()" is used to access one constructor from another "within the same class". Depending on the parameters supplied, the suitable constructor is accessed.
24/2
/201
4
Ebtsam Abdelhakam
18
![Page 19: Supervisor Ebtsam AbdelHakam ebtsamabd@gmail.com Department of Computer Science Najran University 24/2/2014 Ebtsam Abdelhakam 1.](https://reader035.fdocuments.us/reader035/viewer/2022070412/5697bf7d1a28abf838c8473f/html5/thumbnails/19.jpg)
Rules of using this()
» If included, this() statement must be the first one in the constructor. You cannot write anything before this() in the constructor.
» With the above rule, there cannot be two this() statements in the same constructor (because both cannot be the first).
» this() must be used with constructors only, that too to call the same class constructor (but not super class constructor).
24/2
/201
4
Ebtsam Abdelhakam
19