Method overriding allows us to provide our own implementation of a method in a child class.
"Overriding and Hiding Methods (The Java™ Tutorials > Interfaces and Inheritance)" ( http://bit.ly/Zn8eop )
"Polymorphism (The Java™ Tutorials > Learning the Java Language > Interfaces and Inheritance)" ( http://bit.ly/PYQDIV )
Showing posts with label Method. Show all posts
Showing posts with label Method. Show all posts
Monday, March 4, 2013
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.
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.
Friday, December 21, 2012
member access operator (.)
In Java, the dot (.) is an operator called the member access operator.
This operator is used to separate the class/object name from a method name.
For instance, an object called playerOne that has a method called fight:
playerOne.fight()
This operator is used to separate the class/object name from a method name.
For instance, an object called playerOne that has a method called fight:
playerOne.fight()
Wednesday, September 5, 2012
Method Overloading
You may have many methods with the same name in a class. This is known as method overloading.
The rule is that each parameter list must be different. This applies to constructors as well.
doFoo(int param1, int param2)
doFoo(String param1, int param2)
Correct: Same name, unique parameter list.
doFoo(int param1, int param2)
doFoo(int param1, int param2)
Incorrect: Same name, same parameter list.
Wednesday, July 25, 2012
Polymorphism - one name, many forms.
"Polymorphism (The Java™ Tutorials > Learning the Java Language > Interfaces and Inheritance)" ( http://bit.ly/PYQDIV )
Labels:
Inheritance,
Links,
Method,
Overloading,
Polymorphism
Friday, May 18, 2012
The 'static' keyword
Download the code
Static variables & methods are associated with the class rather than an instance of an object of that class.
Usually, the data contained within an object, belongs only to that instance.
If you want all instances of a class to share some data, use a static variable. A static variable belongs to the class, rather than an instance the class (object).
The example below uses a static variable to keep count of the number of Widget objects that have been created.
The Widget class contains a single static variable called _numberOfWidgets. No matter how many instances of Widget are created, there will be a single value associated with the _numberOfWidgets variable.
A static method is provided to set the value of _numberOfWidgets and a static method is provided to get the value of _numberOfWidgets. In this example, the constructor calls the set method and in turn, the set method increments the value.
In the TestWidgetClass below, a number of Widget instances are created. Note that the call to the get_numberOfWidgets method is made by referencing the class directly, rather than by instance of the class.
And the output is:
"Understanding Instance and Class Members (The Java™ Tutorials > Learning the Java Language > Classes and Objects)" ( http://bit.ly/KmbYd7 )
In a UML class diagram, Static variables & methods are represented with an underline.
Here's a cat to keep you calm:
Labels:
cat,
Class diagram,
Method,
Reserved keywords,
Static,
UML,
Variables
Parameters / Arguments
See also:
"Passing Information to a Method or a Constructor (The Java™ Tutorials > Learning the Java Language > Classes and Objects)" ( http://bit.ly/XxwPUf )
Thursday, May 10, 2012
Getters & setters
"Mutator method - Wikipedia, the free encyclopedia" ( http://bit.ly/K3zSdR )
Tuesday, March 27, 2012
Methods: Procedures & Functions
DOWNLOAD THE CODE: MethodsPkg
There are two types of methods:
A method signature is a contract which specifies:
Procedure
What it does: executes processing statements.
Example:
Function
What it does: executes processing statements and returns a value to the calling procedure.
Example:
There are two types of methods:
- Procedure
- Function
A method signature is a contract which specifies:
- visibility (can it be seen externally?)
- what type of data it will return
- what it will do (the name)
- which data, if any, it requires to perform it's task
visibility | return data type | method name | (parameters if any) {
}
Procedure
What it does: executes processing statements.
Example:
The method signature for a procedure always contains the keyword void (meaning nothing/empty). This is as opposed to a function, which always returns a value. Read on...
Function
What it does: executes processing statements and returns a value to the calling procedure.
Example:
The method signature for a function always contains a datatype to indicate the type of data that will be returned from the function.
The final statement in a function begins with the return keyword, followed by the data that will be returned. In this case, a String.
Methods may also require incoming values called parameters (sometimes called arguments) which are placed within the parenthesis following the method name.
Subscribe to:
Posts (Atom)







