In this tutorial, we will discuss different ways of method definition with sample codes. This tutorial covers how objects are passed in a method and an object is returned by a method.
Passing Object as Parameters
An object of a class can be passed in a method as an argument. More than one objects of one class or different classes can also be passed in the method as parameters (according to the requirements). In the following example we will show how to pass an object as parameter.
Code ComplexNum.java
Source code of Test.java
Returning an Object
A method can also return an object. In the following example, we are showing that add method receives two objects of ComplexNum class and it returns an object of ComplexNum class.
Source code of ComplexNum.java
Source code of Test.java
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.