Chapter 2: Class and Objects
Chapter 2: Class and Objects
You can call an object by setting a variable to that object called an "Object Reference."
A class describes the general "layout" of a certain type of object. Then, individual,
specific objects can be created as part of a class. An object is an instance of a class.
Some Vocabulary:
Data Fields or Instance Variables: Where the current state of an object is stored,
essentially that object's attributes.
Methods: Provide both the behaviors exhibited by the object and the operations that
manipulate the object.
Encapsulation: Combining and object's data and methods into a single unit called a
class.
Static: A static variable or method, essentially, belongs not to a particular instance of the
class, but actually to the class itself. This means that, for example, a static method can
be called without refernce to an object.
Thus, if there is an add(a, b) function, it can be called within the same class as
add(a, b) instead of the following code below:
/* Methods */
public add(int a, int b) {
int c = a + b;
System.out.print(c);
}
}
Methods:
Headers:
All method headers are created in this format: public void withdraw String password,
double amount) {}
In this example, public is an access specifier. This is because it decides who gets
access to the method.
Next, the void is the return type. It specifies what kind of output the method gives.
Finally, the String password, double amount) is just the parameter list.
Constructors
Constructors create an object of the class. The constructor's name is always the same
as the name of the class. The reason for this is because it specifies the creation of an
object of the class. It has no return type.
Another type of constructor actually has parameters, and uses the information
gained from the parameters' inputs in order to set the instance variables of a
certain object to the values of those parameters.
Important to Note: "b" and "c" in the code snippets below are NOT the objects
themselves. Instead they are simply references to the objects.
Accessors
An accessor method accesses a class object without altering the object. It simply
returns some information about the object.
It, too, follows the method header format, and this time, unlike a constructor, it actually
has a return type.
Dot Operator:
The dot operator ( . operator) indicates that getBalance() is a method of the class to
which b1 and b2 belong, so thus, you can state, b1.getBalance() or b2.getBalance()
Mutators
A mutator method changes the state of an object by modifying at least one of its
instance variables.
It has parameters, and inside of a mutator, there is usually some kind of function.
Sometimes, it is a for, if, if - else, while, etc. function to implement and change one of the
instance variables of that class.
Or, the dot operator can follow the class name, preceding the method name. This works
as well.
Instance Method
Instance Method: This is a type of method that works, and "belongs" to an instance of a
class. Thus, it works and "belongs" to an object. In order to call an instance method, you
must use the dot operator following the object name.
The main method must always be static because it is located at the start of execution of
the program, and is at a time where there have not been any objects created. Thus, the
main method must always be static.
Method Overloading
Overloaded Methods are two or more methods in the same class that have the same
name but different parameter lists.
/** The signatures of these functions do not care about return types, only about the parameter list
and the function name. That is all that defines the signatures. The signature list follows below */
product(int);
product(double);
product(int, int);
Scope
The instance variables, static variables, and methods of a class belong to that class's
scope.
A certain component's scope starts at the first brace - { - and ends at the second brace
-}-.
It is important to note that a local variable, which is defined inside of a method, has
a scope of that method. Its scope, AKA area of operation, extends from the first
brace to the second brace of that method's definition.
Local Variables take precedence over instance variables with the same name. However,
doing this is bad practice, and creates ambiguity for the programmer.
Example: Let's say there is a method call obj.doSomething("Mary", num). Here, "Mary,"
and num, are explicit parameters while obj is an implicit parameter.
Example: For example, you can use this to print the object itself. You can say
System.out.println(this) and it will call the toString(); function, which will print the object.
Example: Third example, let's say you would like to simply create a method called
"deposit" that adds to a bank account's balance. If the bank account has an instance
variable called "balance," the method's statements do not need to be this.balance +=
amount;, and instead, they can be balance += amount;. The use of "this" here is
unneeded.
Example: Let's assume that you are creating a local variable inside of a constructor, but
that local variable has the same name as an instance variable defined at the beginning of
the class. This is bad practice, but what you could do to handle it is use the keyword
"this." Look at the following code segment:
Reference Data Types: Include objects, arrays. In a reference data type, a variable data
slot is created, but the actual data is not stored there. Instead, a reference, or simply
the address, for the data is stored. Thus, for example, in the statement Date d = new
Date(2, 17, 1948; the memory slot d is being created, which does not store the new
object, but stores the address for the object d.
This means that you can have multiple variables referring to the same object or
array.
Date birthday = d; does not create a clone object of d called object birthday. It,
instead, creates a memory slot of birthday that refers to the object d.
In order to create the clone, the new keyword must be used, and the object -
declaring process must go through. For example, the programmer can say Date
birthday = new Date(d.getMonth(), d.getDay(), d.getYear();
This type of uninitialized object variable is called a null reference or null pointer. Any
attempt to invoke an instance method with a null reference may cause the program to
terminate with a NullPointerException.
Fail to initialize a local variable: This will lead to the compiler giving a compile-time
error.
Fail to initialize an instance variable of a class: The compiler will provide reasonable
default values, such as 0 for int and false for booleans.
Fail to initialize a reference instance variable of a class: Compiler will set it to null. If
invoked, it will result in a NullPointerException.
Actual Parameters: When a method is actually called, the method call will have variables
in the parameter list that are the actual parameters.
When the method is exited, the local memory slots of the parameters are erased.
Thus, it means that you cannot repalce an object with another one, as you can't change
the reference that was passed. However, you can change and affect the object being
dealt with, and its state, in many cases.
Example: A method can be called that has the parameters of an object. Thus, now, there
is a local reference to the object. Then, the object can be manipulated or changed by
this local reference. When all is said and done, the local reference, as well as all the local
parameters, are deleted. However, the changes on the object are still there.
However, this will not work, because after the method is over, the local variable set
to the better BankAccount object will disappear. In order to fix this, the programmer
needs to: