AP Computer Science A: Object-Oriented Programming Guided Notes

AP Computer Science A: Object-Oriented Programming Guided Notes

Procedural Abstraction

Objectives
  • Represent the design of a program using natural language or diagrams indicating the classes in the program, including all attributes and behaviors.

  • Develop code to call class methods and determine the result of those calls.

  • Describe the functionality and use of code through comments.

  • Explain where variables can be used in the code.

Key Concepts

Abstraction
  • Definition: Simplifying something and reducing its complexity by focusing on its main idea.

Procedural Abstraction
  • Definition: When programmers reuse methods/functions/procedures without knowing every single detail on how they work.

Data Abstraction
  • Definition: Simplifying how data is represented in a program.

Class Methods vs Instance Methods
  • Class Methods:

    • Methods associated with classes, not instances.

    • Dot notation is not required if calling methods within the same class.

  • Instance Methods:

    • Created from a class's instance.

    • Requires creating an object, referencing its name with dot notation, and calling the method.

Scope
  • Definition: How accessible something is within a program.

    • Example: A variable created inside a loop is local and cannot be accessed by any code outside of that loop.


Constructing Objects

Objectives
  • Explain the relationship between a class and an object.

  • Develop code to create an object by calling a constructor.

  • Identify the correct constructor being called using its signature.

  • Develop code to declare instance variables for attributes to be initialized in the body of the constructors of a class.

  • Develop code to define behaviors of an object through methods written in a class using primitive values and determine the result of calling these methods.

Key Questions and Terms
Example Objects in the Real World
  • Clothes, items, books, food, etc.

Procedural Programming vs Object-Oriented Programming
  • Procedural Programming:

    • Programming centered around procedures, breaking programs down into methods.

  • Object-Oriented Programming:

    • Programs designed to model real-world or virtual things that can communicate and interact with each other.

    • A class serves as a blueprint defining an object's attributes and behaviors.

Attributes
  • Definition: Like a noun; attributes define the "what" of something, holding the data for an object.

Behaviors
  • Definition: Like a verb; behaviors depict the "how" and describe the actions that an object carries out.


Overloading and Encapsulation

Objectives
  • Describe how to call methods.

  • Identify the correct constructor being called using its signature.

  • Develop code to designate access and visibility constraints to classes, data, constructors, and methods.

  • Define behaviors of an object through methods, and develop class behaviors through class methods.

Key Questions and Terms
Relationship of Attributes to an Object
  • Definition: A "has a" relationship, indicating attributes reveal the properties of an object or its features.

Purpose of the Reserved Word "new"
  • Definition: Creates a new object of the respective class using its constructor.

Default Constructor
  • Definition: A constructor that does not have any parameters.

  • Example: In an Animal() class, the default constructor is Animal().

UML (Unified Modeling Language)
  • Definition: A standard for visually representing a program's design and documentation through diagrams.

Class Diagram
  • Usage: Depicts a class's key properties and its relationships with other classes.

Instance Variables
  • Definition: Another term for attributes; variables referenced by specific objects that hold data and belong to the object.

Default Values for Instance Variables
  • Integers: 0

  • Doubles: 0.0

  • Booleans: false

  • Object References: null

Object's State
  • Definition: The current value of an object's instance variables.


Object References

Objectives
  • Explain the relationship between a class and an object.

  • Develop code to declare variables for reference types.

  • Compare object references using Boolean expressions.

  • Develop self-referencing expressions and determine results.

Key Questions and Terms
Overloading
  • Definition: Using the same name for different methods/constructors as long as their parameter lists are different.

Encapsulation
  • Definition: Keeping data private.

Accessor Method
  • Definition: A method that can access private instance variables but cannot change them; also known as a getter.

Mutator Method
  • Definition: A method capable of changing a private instance variable's value; also known as a setter or modifier.

Client Class vs Implementation Class
  • Example:

    • Planet: Implementation Class

    • PlanetTester: Client Class

Key to Object-Oriented Programming
  • The ability to construct multiple objects.


Class Variables and Methods

Objectives
  • Develop code to call class methods and determine the results.

  • Define behaviors of a class through class methods.

  • Declare class variables that belong to the class.

Key Questions and Terms
Method for Comparing Objects
  • Method: equals()

Passing Reference Types as Arguments
  • Definition: Passing a reference type tells a method the address of an object; changes to the original object also affect the reference type.

Java Behavior with Primitive Data Types
  • Process: Java makes a copy of a value to initialize the parameter; changes to the original value do not affect the parameter.

Scope of a Variable
  • Definition: The accessibility of a variable within a piece of code.

this Keyword
  • Definition: A special variable that references the current object; useful for distinguishing between two variables with the same name.

Static vs Non-Static
  • Static Methods: Can be executed without the prior existence of an object.

  • Non-Static Methods: Can only be called if an object exists.

Another Name for Static Methods
  • Terms: Class methods.

Formatting a Constant Variable
  • Recommendation: Capitalize them.

Purpose of the Final Keyword
  • Definition: Ensures that a variable remains constant and cannot be changed.