Rycal.
← All AP courses
AP Computer Science A · Cram sheet

Unit 3 · Class Creation

10–18% of the AP exam 41 key terms

● Core concept  ·  ○ Supporting concept

3.1 Abstraction and Program Design

Abstraction ● (core concept) — Abstraction is the process of reducing complexity by focusing on the main idea. By hiding details irrelevant to the question at hand and bringing together related and useful details, abstraction reduces complexity and allows one to focus on the idea.

Data abstraction ● (core concept) — Data abstraction provides a separation between the abstract properties of a data type and the concrete details of its representation. Data abstraction manages complexity by giving data a name without referencing the specific details of the representation. Data can take the form of a single variable or a collection of data, such as in a class or a set of data.

Attribute (class design) ● (core concept) — An attribute is a type of data abstraction that is defined in a class outside any method or constructor.

Instance variable ● (core concept) — An instance variable is an attribute whose value is unique to each instance of the class. Instance variables belong to the object, and each object has its own copy of the variable. A class variable is an attribute shared by all instances of the class.

Method decomposition ● (core concept) — Through method decomposition, a programmer breaks down larger behaviors of the class into smaller behaviors by creating methods to represent each individual smaller behavior. A procedural abstraction may extract shared features to generalize functionality instead of duplicating code. This allows for code reuse, which helps manage complexity.

Generalized procedures via parameters ● (core concept) — Using parameters allows procedures to be generalized, enabling the procedures to be reused with a range of input values or arguments.

Changing method internals ● (core concept) — Using procedural abstraction in a program allows programmers to change the internals of a method (to make it faster, more efficient, use less storage, etc.) without needing to notify method users of the change as long as the method signature and what the method does is preserved.

Designing a class before implementing ● (core concept) — Prior to implementing a class, it is helpful to take time to design each class including its attributes and behaviors. This design can be represented using natural language or diagrams.

3.2 Impact of Program Design

System reliability ● (core concept) — System reliability refers to the program being able to perform its tasks as expected under stated conditions without failure. Programmers should make an effort to maximize system reliability by testing the program with a variety of conditions.

Impact of programs on society ● (core concept) — The creation of programs has impacts on society, the economy, and culture. These impacts can be both beneficial and harmful. Programs meant to fill a need or solve a problem can have unintended harmful effects beyond their intended use.

Open source and intellectual property ● (core concept) — Legal issues and intellectual property concerns arise when creating programs. Programmers often reuse code written by others and published as open source and free to use. Incorporation of code that is not published as open source requires the programmer to obtain permission and often purchase the code before integrating it into their program.

3.3 Anatomy of a Class

Data encapsulation ● (core concept) — Data encapsulation is a technique in which the implementation details of a class are kept hidden from external classes.

public and private ● (core concept) — The keywords public and private affect the access of classes, data, constructors, and methods. The keyword private restricts access to the declaring class, while the keyword public allows access from classes outside the declaring class.

Class declaration conventions ● (core concept) — In this course, classes are always designated public and are declared with the keyword class. Constructors are always designated public.

Private instance variables ● (core concept) — Access to attributes should be kept internal to the class in order to accomplish encapsulation. Therefore, it is good programming practice to designate the instance variables for these attributes as private unless the class specification states otherwise.

Public vs. private methods ● (core concept) — Access to behaviors can be internal or external to the class. Methods designated as public can be accessed internally or externally to a class, whereas methods designated as private can only be accessed internally to the class.

3.4 Constructors

Object state ● (core concept) — An object's state refers to its attributes and their values at a given time and is defined by instance variables belonging to the object. This defines a has-a relationship between the object and its instance variables.

Constructor initializes state ● (core concept) — A constructor is used to set the initial state of an object, which should include initial values for all instance variables. When a constructor is called, memory is allocated for the object and the associated object reference is returned. Constructor parameters, if specified, provide data to initialize instance variables.

Copying mutable constructor parameters ● (core concept) — When a mutable object is a constructor parameter, the instance variable should be initialized with a copy of the referenced object. In this way, the instance variable does not hold a reference to the original object, and methods are prevented from modifying the state of the original object.

Default constructor ● (core concept) — When no constructor is written, Java provides a no-parameter constructor, and the instance variables are set to default values according to the data type of the attribute. This constructor is called the default constructor.

Default values of attributes ● (core concept) — The default value for an attribute of type int is 0. The default value of an attribute of type double is 0.0. The default value of an attribute of type boolean is false. The default value of a reference type is null.

3.5 Methods: How to Write Them

void method header ● (core concept) — A void method does not return a value. Its header contains the keyword void before the method name.

Non-void method header ● (core concept) — A non-void method returns a single value. Its header includes the return type in place of the keyword void.

Return by value ● (core concept) — In non-void methods, a return expression compatible with the return type is evaluated, and the value is returned. This is referred to as return by value.

return keyword ● (core concept) — The return keyword is used to return the flow of control to the point where the method or constructor was called. Any code that is sequentially after a return statement will never be executed. Executing a return statement inside a selection or iteration statement will halt the statement and exit the method or constructor.

Accessor method ● (core concept) — An accessor method allows objects of other classes to obtain a copy of the value of instance variables or class variables. An accessor method is a non-void method.

Mutator (modifier) method ● (core concept) — A mutator (modifier) method is a method that changes the values of the instance variables or class variables. A mutator method is often a void method.

Method parameters receive values ● (core concept) — Methods with parameters receive values through those parameters and use those values in accomplishing the method's task.

Primitive parameter is a copy ● (core concept) — When an argument is a primitive value, the parameter is initialized with a copy of that value. Changes to the parameter have no effect on the corresponding argument.

3.6 Methods: Passing and Returning References of an Object

Passing an object reference as a parameter ● (core concept) — When an argument is an object reference, the parameter is initialized with a copy of that reference; it does not create a new independent copy of the object. If the parameter refers to a mutable object, the method or constructor can use this reference to alter the state of the object. It is good programming practice to not modify mutable objects that are passed as parameters unless required in the specification.

Returning an object reference ● (core concept) — When the return expression evaluates to an object reference, the reference is returned, not a reference to a new copy of the object.

Private access of parameters ● (core concept) — Methods cannot access the private data and methods of a parameter that holds a reference to an object unless the parameter is the same type as the method's enclosing class.

3.7 Class Variables and Methods

Class method restrictions ● (core concept) — Class methods cannot access or change the values of instance variables or call instance methods without being passed an instance of the class via a parameter. Class methods can access or change the values of class variables and can call other class methods.

Class variable ● (core concept) — Class variables belong to the class, with all objects of a class sharing a single copy of the class variable. Class variables are designated with the static keyword before the variable type.

Accessing public class variables ● (core concept) — Class variables that are designated public are accessed outside of the class by using the class name and the dot operator, since they are associated with a class, not objects of a class.

final ● (core concept) — When a variable is declared final, its value cannot be modified.

3.8 Scope and Access

Local variable ● (core concept) — Local variables are variables declared in the headers or bodies of blocks of code. Local variables can only be accessed in the block in which they are declared. Since constructors and methods are blocks of code, parameters to constructors or methods are also considered local variables. These variables may only be used within the constructor or method and cannot be declared to be public or private.

Local variable shadows instance variable ● (core concept) — When there is a local variable or parameter with the same name as an instance variable, the variable name will refer to the local variable instead of the instance variable within the body of the constructor or method.

3.9 this Keyword

this ● (core concept) — Within an instance method or a constructor, the keyword this acts as a special variable that holds a reference to the current object — the object whose method or constructor is being called.

Passing this as an argument ● (core concept) — The keyword this can be used to pass the current object as an argument in a method call.

this and class methods ● (core concept) — Class methods do not have a this reference.