Java Beginners - Java For Beginners Java Methods Rating: 0.0/5 (0 votes cast) | Level : Beginners Author : Arunkumar S Download Source : Not Avaliable
Java Method Overloading
Method Overloading Each method in a class is uniquely identified by its name and parameter list. What this means is that you can have two or more methods with the exact same name, but each with a different parameter list. This is a powerful feature of the Java language called method overloading. Since method names typically represent an action, this allows you to define how to perform the same action on different types of inputs. Here is an example of overloading a method max() in order to calculate a maximum value for different combinations of inputs: public static int max ( int x, int y ) { // calculate max using two ints }
public static double max ( double x, double y ) { // calculate max using two doubles }
Method Overriding If a child class extends a parent class, the child inherits all of the methods and attributes of the parent class. So, if a parent class contains a method reorder(): public int reorder(int x, int y) { //some parent implementation return x; } The child inherits this method. But, the child may decide to override the parent's implementation by redefining the method: public int reorder(int x, int y) { //new child implementation return y; }
Notice that the child returns y whereas the parent returns x. This feature of Java enables a powerful, object-oriented technique called polymorphism. Note: Although a child class inherits all methods and attributes of their parent class, the methods and attributes must have the correct visibility in order for the child to access them.
Method Visibility A method's visibility (also know as its access scope) defines what objects can invoke it and whether subclasses can override it. The four possible visibility modifiers are: public, protected, private, and no modifier. Keeping an object's methods as hidden as possible helps simplify the object's published API. Make a method only as visible as it needs to be. For instance, if a method is intended to be overridden by a subclass but never invoked by client code, make the visibility protected instead of public. If a method should never be invoked by another class and is not meant to be overridden, make it private. Visibility of variables is covered in Java Variables.
| Method Visibility | | Modifier | Can be Accessed By | | public | any class | | protected | owning class, any subclass, any class in the same package | | No Modifier | owning class, any class in the same package | | private | owning class | Parameters are Passed by Value In Java, when a value is passed into a method invocation as an argument, it is passed by value. This is unlike some other programming languages that allow pointers to memory addresses to be passed into methods. When a primitive value is passed into a method, a copy of the primitive is made. The copy is what is actually manipulated in the method. So, the value of the copy can be changed within the method, but the original value remains unchanged. When an object reference or array 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. But, if it reassigns a new object or array to the reference, the reassignment only affects the copy, not the original reference. As an example, if you pass a URL object reference named myURL into a method invocation, the method can call myURL.set(someNewUrl) to change the URL's attributes, but if it tries to reassign a new URL object to the myURL reference, the change will only be in effect for the scope of the method invocation. In other words, myURL = new URL() changes the memory address that the copy of myURL points to, not the memory address of the original myURL. In this tutorial we have covered the basics of using and writing methods in Java. This, combined with previous tutorials on variables and objects, provides the foundation for more advanced concepts in object-oriented programming and design patterns. < Previous 1 | 2 | 3 |
Discussion about this tutorial
 Start a new Discussion | Read All Discussion
|