public class Account{

//class properties/attributes

private String custId;

private double balance;

//constructor method

public Account(String id){

this.id = id;

this.balance = 0;

}

//another constructor method (method overloading)

public Account(String id, double balance){

this.id = id;

this.balance = balance;

}

//accessor methods

public String getID(){

return this.id;

}

public String getBalance(){

return this.balance;

}

//mutator methods

public void setBalance(int newBalance){

this.balance = newBalance;

}

}

Method signatures

Method signatures define/describe:

  • the name/identifier of the method
  • the parameters of the method (identifier and data type)
  • the return type of the method eg void or int etc

Fun Fact!

When we declare an array of Objects, for example Strings or Accounts or Elephants, Java conveniently uses null as a placeholder.

Account[] accounts = new Account[9];

Remember, with integer arrays it uses 0, with double arrays it uses 0.0, with boolean arrays it uses...

Objectives

Students will be able to:

  • understand the basic concept of method overriding
  • understand how to use the super keyword to call superclass methods when method overriding is used

TESTING

Alpha testing is performed on code eg classes and functions, during program development and is done by the developers.

Beta testing happens when a small set of representatives from the customer/client test the program/product (they don't see the code).

If Beta testing is not successful, the developers have to return to their office and fix any bugs or make any necessary changes, with more Alpha testing.

When Beta testing is successful, the product can be installed/implemented on client machines using a variety of techniques:

  • direct implementation
  • phased implementation
  • pilot running
  • parallel implementation

null

null

Remember    ?

accounts is a reference to an array of Account objects.

is an abstraction of an Account in the form of a class.

Note: there is an example of method overloading here.

Here is the code in another class, Main, which constructed the above array of Account objects:

Account[] accounts = new Account[9];

Processing arrays of objects

A method in the class Main, countRich, will process the array, accounts.

It will return a count of all Accounts which have a balance greater than the parameter balance

Let's assume that accounts is a global variable and visible to all methods in the Main class.

Complete the code to process the array:

public static countRich(double ){

int count = ;

for(int i = 0; i<accounts.; i++){

if(accounts[]. > balance){

count = count + 1;

}

}

return

}

Runtime Errors

If we compile this program, we will not get a compiler error.

However, if we run this program, we might get a runtime error.

This is because if there is a null in the array, then the if statement will crash the program.

For example, look at the animation above.

accounts[3] references null (digital nothing, a convenient placeholder if an object is not available).

so accounts[3].getBalance() cannot be executed because null does not have a getBalance() method associated with it!

Checking for null

Especially when processing arrays of objects like Accounts or Strings or Elephants or... we need to write code to check for null.

To fix the above problem, we need to make the if statement a bit smarter:

if(accounts[i]!=null && accounts[i].getBalance() >balance){

Note: the check for null has to happen before further processing.

Because we are using AND logic, if the first statement if false, Java will not bother to execute the second half of the statement because false and anything is false...

Concept check!

When code, for example a method, processes an array of objects, it is more than likely that the array will have nulls in it.

Attempting to process an array without checking for nulls may result in a runtime error!

Runtime errors are very awkward because they can happen after successful compilation of the program (just when we assume everything is fine!)

This is why thorough testing eg is extremely important when designing computer programs.

Exercises

Click the exercises button and see if you can complete the challenges.

Glossary

object

property

method

create an object/instance of a class

instantiate an object

encapsulate

access

mutate

abstraction

Tags

methodsyntax overriding overloading instantiate instance super parameterattribute