Using ‘objects’ as parameters


AutomationChinnu

So far we’ve only been using simple types like int, double etc as parameters in methods and constructors.But we can  pass objects to methods & constructors and also use objects as parameters in the methods & constructors.

The following example statements will show how to pass objects to methods & constructors :

Example: Passing Object ‘box2’ to Box( ) constructor in Box Class

Box box1 = new Box(box2);

  •    box1 and box2 are objects of Box class
  •    Here in this example we’re passing the box2 object to Box( Box object) constructor in Box class

Example: Passing Object ‘box2’ to equal( ) method in Box Class

boolean x =  box1.equal(box2);

  • box1 and box2 are objects of Box class
  • x is the boolean type variable which can store either true or false
  • equal( )  is a method of Box class which has the object parameter i.e. public boolean equal(Box object)

Lets…

View original post 479 more words

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s