Objects and Classes

In Java, almost every program you write has one thing that is being created or modified by the program. This thing, and the operations that manipulate it are known as an object. Objects have data (fields) and behavior (methods) and do the work in an object-oriented program.

Objects are created by classes. A class defines the data (fields) and behavior (methods) for all objects of that class, and you can create many objects from the same class.

An object is characterized by its state and behavior. For example, a car has a state which is described by its model, manufacturer and year, while its behaviour could be accelerating. Java represents objects as variables in a program, known as an object reference.

Classes, on the other hand, are blueprints for implementing objects of a given type. (Barrons, 2018). If we consider cookie dough to be the memory being allocated to objects, a cookie cutter is the class which provides a way for use to implement an object. Through encapsulation, an object’s data and methods are combined into this class.

Public vs Private vs Static

In Java, we have different types of classes, methods and variables, which set whether they are accessible or not to client programs or not.

Public - Class is usable by all client programs, however implementation details or private instance variables and methods cannot be accessible.

Private - Methods and variables set as private can only be accessed by methods of the same class.

Static - Static variables contain a value that is shared by all instances of the class, so the memory is only allocated once.

Accessor vs Mutator Methods

To access objects without altering them, we use accessor methods, which return specific information about the object. Here is an example of a basic method for the Car class which returns the model of the car

 public String getModel()
{
	return model;
}

A mutator method changes the state of an object, by modifying at least one instance variable.



Static Methods vs Instance Methods

Instance methods are those which operate on an individual objects of a class (Barron's AP CS book). These are the constructors, accessors and mutators in our class. Static methods, on the other hand, perform an operation for the entire class instead of individual objects.

A good example of this is if we create an Employees class with the private instance variables name and employeeCount. Here is the getEmployeeCount static method:

 public static int getEmployeeCount()
{
	Return employeeCount; 
}
                        
                        
The above method works because the employeeCount variable is set as a static variable. Other examples would be a getInterestRate() method, as the interest rate is applied to all bank accounts in one class.

Method overloading

Overloaded methods are 2+ methods in the same class or subclass of that class which have the same name but different parameter lists. Take a look at the three variations of the product() method, product(int), product(double) and product(int, int).

We use overloaded methods when we have a series of methods which do similar tasks but have minor differences, to save time with creating multiple, confusing method names.

Reference vs Primitive Data Types

Built-in data types in Java, such as int, double, boolean and char, are known as primitive data types. On the other hand, objects and arrays are known as reference data types, such as String or int[].

When we declare a primitive data type, such as int num1, they can be thought as memory slots in place. On the other hand, declaring a reference data type requires us using new. The declaration creates a reference variable that refers to a specific object.

The value of the reference variable is the address in memory of the object. If we declare a reference as null, it means it is uninitialized, and becomes a null reference.

How are parameters passed to methods?

In Java, parameters are only passed by value. This means, that for primitive data types, when a method is called, a new memory slot is allocated for each new parameter.

The value of each argument is copied into the new memory slot for each parameter. Any changes made to the parameters in the methods will not affect their value. The same applies to objects, but instead the address is copied, not the value.

Object-Oriented Program Design (Barrons AP CS



Steps in Object-Oriented Design

1. Identify classes to be written

2. Identify behaviors (i.e: methods for each class)

3. Determine the relationships between classes

4. Write the interface (public method headers) for each class

5. Implement the methods

Inheritance Relationships: Looking at classes with common behaviours, known as is-a relationships Composition Relationships: Known as the has-a relationship - usually when one class contains an instance variable whose type is the other class.

UML Diagrams

United Modeling Language (UML) diagrams are a great way to keep track of the relationships between classes, and show which classes inherit each other.

Some rules for creating UMLs include:

1. Represent Classes with Rectangles

2. Use angle brackets to indicate if a class is either abstract or an interface.

3. Is-a relationships are shown with an open up arrow, where the arrow is dotted if it involves an interface.

4. Has-a relationships between classes are marked with a down/sideways arrow indicating composition.