Showing posts with label Behavior. Show all posts
Showing posts with label Behavior. Show all posts

Monday, February 25, 2013

Methods and method parameters

Consider a method (also called a behaviour) for logging on to a network. When a user logs on, we could reasonably assume that the user must provide a username and password. A method for logging on could look like this:


This method signature can be broken down as follows:

public - The visibility of the method. A method that is public (as opposed to private) can be seen by other classes.

void - This indicates that the method will not return a value. Some methods return values (as in the case of the Scanner’s nextInt() function).

login - This is the name of method. Method names are named in the same way as variables in that they should start with a lower case letter. If the method name contains serveral words, each subsequent word starts with an uppercase letter. Method names must be unique - except where method overloading is used.

(String userName, String password) - This is the parameter list for method. When a method requires some data to perform its task, a list of that data is specified. This parameter list contains 2 parameters (although a parameter list may contain more, less or no parameters). 

Each parameter has a name, a type and is separated by a comma.

Monday, March 19, 2012

What is a Class? What is an object?

A class is a definition of all state/attributes & behavior belonging to an entity. The following figure is a UML class diagram for an entity called Player. The middle compartment lists each state/attribute of Player and the bottom compartment lists each behaviour of Player.

UML class diagrams are used to design classes. This design is then used to inform the code. It's like a blueprint for building a house.

Objects are created from classes - many objects may be created from a single class - in the same way as one blueprint can build many houses. The following figure is a UML object diagram showing the Player class along with 3 Player objects.

Notice that while each object has a strength and agility attribute, the values for these attributes are unique from object to object. From this diagram, we can see the state of each object.

The behaviours are not listed in the object diagram because all objects share the same behaviours.

"What Is a Class? (The Java™ Tutorials > Learning the Java Language > Object-Oriented Programming Concepts)" ( http://bit.ly/FUAjmF )