When trying to store data on something complex that requires lots of variables, maybe of different types (strings, integers, doubles) we need a good way of organizing everything.
For example, if we wanted to write code involved in managing a restaurant we might want to store:
Strings (name of the restaurant, managers, waiters/waitresses)
Integers (number of employees, number of tables, number of customers)
Doubles (amount earned, amount spent, supply costs, salary costs)
All of this data may need to be stored in arrays, array lists, or hashmaps. Also, there may be methods created that deal specifically with manipulating and updating this data.
What happens if we want to collect data like this on a whole chain of restaurants? We can stay organized by using Classes and Objects.
We have seen a few Classes that are built into JAVA, like the String Class and the Math Class.
Objects called strings and integers, etc. use these classes to get access to built-in methods contained in the class, like the string.length method.
We can also create our own Classes for Objects that we want to describe like a restaurant object.
Creating a Class
Class files do not themselves execute methods, or create and manipulate any data, they simply define and initiate methods for objects that can be called in other areas or files of your program.
Step1: Initiate the Class
The following syntax initiates a class called Restaurant:
public class Restaurant {
public indicates that anyone can use this Class
Names for classes typically start with a capital letter
Step2: Initiate the Class Field Variables
Field variables or Instance variables are used inside the class by methods you want to create in the class. For the restaurant example, we will create field variables for the following:
name : name of the restaurant , string type
earned: the total amount earned , double type
costs: the total costs incurred , double type
The syntax for initiating these class field variables looks like the following:
public class Restaurant {
String name;
double earned;
double costs;
Step3: Initiate the Class Methods
Methods created in the class can reference the field variables or create new ones. Two methods are created in the Restaurant Class:
calculateProfit(double profit) - calculates the total current profit
displayEarned() - prints the Restaurant Name and the total amount earned
The complete syntax for the Restaurant Class looks like the following:
public class Restaurant {
String name;
double earned;
double costs;
//Method to calculate total profit
public void calculateProfit(double profit) {
profit = earned - costs;
return profit;
}
//Get access to println for the method displayEarned
Logic Interface aLogic;
public Restaurant(Logic Interface logic){
aLogic = logic;
}
//Method to print the total earned
public void displayEarned() {
aLogic.println("Total Earned is " + earned);
}
}
Class Concepts
The Classes you create should be general enough that they can be reused, for example the Restaurant Class can be used over and over again for each of the chain restaurants discussed earlier.
The class may interface with either a programmer creating additional code that uses the class, or the end user who may look at the displays and outputs provided from the class. Both of these users should be kept in mind.
Privacy
Field or Instance variables can be set to private as shown in the following syntax to add security and not allow outside programmers or users direct access to them. This is almost always done to ensure that the class and author of the class has total control of how these variables are used. Methods that are only used by the class itself, should also be set to private.
private String name;
private double earned;
private double costs;