Lecture 10 : Classes and Inheritance

In the following series of lectures you will
Please read the textbook Chapter 5 p177-203 or Java Tutorial Creating Classes (and its links)



We are going to use these 3 programs to explain the idea.
Download the programs here
BankAccount.java
, CheckingAccount.java , SavingAccount.java .
or browse the program:
BankAccount , CheckingAccount , SavingAccount .

The class body

public class BankAccount {
<---- class body here
}

The class contains
Notes :


Constructors

Use of constructors


Declaring Member Variables

accessLevel
Lets you control which other classes have access to a member variable by using one of four access levels: public, protected, package, and private. You control access to methods in the same way. 
static
Declares this is a class variable rather than an instance variable. You also use static to declare class methods. 
final
Indicates that the value of this member cannot change. The following variable declaration defines a constant named SPEED_OF_LIGHT , whose value is the speed of light in vacuum = 299,792,458 m/s (meters per second).
final long SPEED_OF_LIGHT = 299792458;
It's a compile-time error if your program ever tries to change a final variable. By convention, the name of constant values are spelled in uppercase letters.
type
Like other variables, a member variable must have a type. You can use primitive type names such as int , float , or boolean . Or you can use reference types, such as array, object, or interface names.
name
A member variable's name can be any legal Java identifier and, by convention, begins with a lowercase letter. You cannot declare more than one member variable with the same name in the same class.


Implementing Methods

 access  return  method   arguments
level   type name |
| | | |
| | | |
V V V V  

public void deposit(int depositAmount) {  <----- method declaration
if(depositAmount >= 0) { 
saving+= depositAmount; <----- method body
}
}

Details of a Method Declaration

A method  declaration can consist access level, return type, method name and arguments. You can add other attributes.
accessLevel
As with member variables, you control which other classes have access to a method using access levels: public, protected and private. 
static
As with member variables, static declares this method as a class method rather than an instance method. 

final
A final method cannot be overridden by subclasses. 

returnType
Java requires that a method declare the data type of the value that it returns. If your method does not return a value, use the keyword void for the return type. 
methodName
A method name can be any legal Java identifier. You need to consider several issues in regards to Java method names. 
( paramlist )
You pass information into a method through its arguments. 

Returning a Value from a Method

Overloading

Passing Information into a Method

double computePayment(double loanAmt, double rate,
double futureValue,
int numPeriods) {
double I, partial1, denominator, answer;

I = rate / 100.0;
partial1 = Math.pow((1 + I), (0.0 - numPeriods));
denominator = (1 - partial1) / I;
answer = ((-1 * loanAmt) / denominator)
- ((futureValue * partial1) / denominator);
return answer;
}

Argument Types

public double method(BankAccount account, int month);
public int sum(int[] intArray);

Argument Names

Pass by Value



this


A Break....

So far you have learned: