Foundations of Class Definitions
Topics
Class Definitions in the Objects-First Approach
1. Introduction to Class Definitions
- Class as a Blueprint: In Java, a class serves as a blueprint for creating objects. It defines the properties (attributes) and behaviors (methods) that the objects created from it will have.
- Objects-First Approach: This approach emphasizes understanding objects and their interactions early in the learning process. By starting with classes that model real-world entities, you can build a solid foundation in object-oriented programming.
2. Starting with a Simple Structure: BankAccount Class
Class Name: BankAccount
This will be the first class in the Bank Application project. The BankAccount class will represent the essential attributes and behaviors of a bank account.
Step 1: Define the Class
- Class Declaration: The class starts with a declaration. In Java, this is done using the
classkeyword followed by the class name.public class BankAccount { // class body }
Step 2: Define Attributes (Fields)
- Attributes: These are the properties of the class, typically representing the state of an object. In the
BankAccountclass, common attributes might includeaccountNumber,accountHolderName, andbalance. - Data Types: For each attribute, choose an appropriate data type. For example,
accountNumbermight be aString,balancemight be adouble, andaccountHolderNamecould also be aString.public class BankAccount { String accountNumber; String accountHolderName; double balance; }
Tester Class with main Method
1. Introduction to the main Method
- Purpose: The
mainmethod is the entry point of any Java application. It is where the execution of the program begins. - Structure: The
mainmethod is always written aspublic static void main(String[] args).
2. Creating a Tester Class
Class Name: BankAccountTester
This class will be used to create instances of the BankAccount class and test its functionality.
public class BankAccountTester {
public static void main(String[] args) {
// Execution starts here
}
}
3. Testing the BankAccount Fields
Step 1: Create an Instance of BankAccount
- Object Creation: To test the
BankAccountclass, you first need to create an instance (object) of it. This is done using thenewkeyword followed by a call to the constructor of theBankAccountclass.BankAccount account1 = new BankAccount();
Step 2: Access and Test the Fields
- Accessing Fields: Initially, you can access and print the values of the fields in the
BankAccountobject to verify that they are initialized correctly. - Printing Field Values: Use
System.out.printlnto display the values ofaccountNumber,accountHolderName, andbalance.System.out.println("Account Number: " + account1.accountNumber); System.out.println("Account Holder Name: " + account1.accountHolderName); System.out.println("Balance: " + account1.balance);
4. Full Code Example for BankAccountTester
public class BankAccountTester {
public static void main(String[] args) {
// Creating an instance of BankAccount
BankAccount account1 = new BankAccount();
// Accessing and printing the field values
System.out.println("Account Number: " + account1.accountNumber);
System.out.println("Account Holder Name: " + account1.accountHolderName);
System.out.println("Balance: " + account1.balance);
}
}
Setting Field Values Directly
When fields are defined with default access, you can set their values directly in any class within the same package. This is a simple way to interact with the data in the object.
Here’s how you can set field values directly in Tester class:
1. Creating an Instance of the Class
- First, you need to create an instance of the
BankAccountclass using thenewkeyword.BankAccount account1 = new BankAccount();
2. Setting Field Values
- After creating the object, you can assign values to its fields directly by accessing them through the object reference (
account1in this case).account1.accountNumber = "123456789"; // Setting the account number account1.accountHolderName = "John Doe"; // Setting the account holder's name account1.balance = 1000.50; // Setting the initial balance - Explanation:
account1.accountNumber = "123456789";: This line sets theaccountNumberfield of theaccount1object to the string"123456789".account1.accountHolderName = "John Doe";: This line sets theaccountHolderNamefield to"John Doe".account1.balance = 1000.50;: This line sets thebalancefield to1000.50.
3. Testing and Verifying
- After setting these values, you can print them out using
System.out.printlnto verify that they have been correctly assigned.System.out.println("Account Number: " + account1.accountNumber); System.out.println("Account Holder Name: " + account1.accountHolderName); System.out.println("Balance: " + account1.balance);
4. Full Code Example for BankAccountTester with setting values and printing
public class BankAccountTester {
public static void main(String[] args) {
// Creating an instance of BankAccount
BankAccount account1 = new BankAccount();
// Setting field values directly
account1.accountNumber = "123456789"; // Setting the account number
account1.accountHolderName = "John Doe"; // Setting the account holder's name
account1.balance = 1000.50; // Setting the initial balance
// Accessing and printing the field values
System.out.println("Account Number: " + account1.accountNumber);
System.out.println("Account Holder Name: " + account1.accountHolderName);
System.out.println("Balance: " + account1.balance);
}
}
Practice Writing Similar Code
Go to Practice and find exercises there.