Chapter 4 (part 2)
Topic Outline Method overloading Usage of this reference Passing parameter: Object as parameter Object as method type Array of objects Composite objects
Method Overloading
Method Overloading
Method Overloading
Method Overloading Example program that has 4 methods which receive different types of parameter list. All methods are named as overloadDemo.
Method Overloading Try It!
The this reference this is a reference variable that refers to the current object. Known as self-referencing pointer To avoid naming conflicts – declare the name of instance variables and local variables are same
this also used to invoke another constructor
Passing Parameter
Passing Parameter The power of method is its ability to work with parameters. When calling a method, arguments need to be provided. When method with parameter is invoked, the value of the arguments is passed to the parameter. Two types of passing parameter: Pass by value Pass by reference
Object As Parameter When an object reference is passed into a method, a copy of the reference is actually manipulated by the method. So, the method can change the attributes of the object. Passing object as an argument is actually passing the reference of the object.
Objects As Parameter
Example