3.x.1 Objects and classes in Java

image_pdfimage_print

As mentioned, Java, does not have good support for singular objects as described in chapter 2. In most cases, objects are instances of classes so to create objects like Account_1010 representing the account of John Smith, we use class Account, which may be defined as follows:

class Account {   Account(Customer ow) {
      owner = ow; 
   }
   Customer owner;
   float balance;
   float interestRate;
   addInterest() {
      balance = balance + (balance * interestRate) / 100;
   }
   deposit(float amount) {
      balance = balance + amount;
   }
   float withdraw(float amount) {
      balance  = balance - amount;
      return balance;
}

Explain class Account, the constructor, introduce main and show how to create instances.
Show class Customer.

final Account account_1010 = new Account(JohnSmithsProfile)

Class Customer, JohnSmithsProfile, references, etc.

© Copyright by Ole Lehrmann Madsen and Birger Møller-Pedersen 2023, 2024, 2025.