The concept of an object
What is an object? Put simply, an object is anything that has both specific attributes that describe it, as well as specific actions that it can perform and can have performed against it. To be more specific, we use the traditional example of a bank account.
A bank account has specific attributes that describe it. It has a balance, for example. It also is of a specific type, i.e, checking or savings. It also may have special aspects such as overdraft protection. All these things, which are considered nouns, that describe the account are properties of the bank account.
What can a bank account do, and what can be done with a bank account? You can make a deposit or withdrawal. You can do more specific things, such as make a transfer to a different account. You can set up overdraft protection or automatic payments. All these things, which are considered verbs, that describe what an account can do are methods of the bank account.
Properties
The properties of an object are nouns that describe it. In the case of the bank account, the following nouns stand out:
- Account type
- Balance
- Interest rate
- Special services
These items are all necessary to manage the account. Some do not change, such as account type, while some change on a regular basis, such as the balance of the account.
Methods
The methods of an object are actions that the object can perform, or can be performed against it. In the case of the bank account, the following verbs stand out:
- Deposit
- Withdraw
- Calculate interest rate
These are all actions that can be done with the account. You might notice that method collaboration and reuse, for example, the deposit and withdraw methods, might both call the calculate interest rate method in order to correctly calculate a final balance. The balance and interest rate properties of the account are both accessed and updated as part of executing the deposit and withdraw method.
The important point here to remember is that everything you need to manage the object is encapsulated within the object itself.
Classes
A class is a compiled object. All your code for a given object, including properties and methods, is placed in a single .java file, and the class name has to match the file name of the Java file. When compiled, a .class file is produced from the .java file.