Classes and Objects: Object-Oriented Programming Fundamentals
Core Object-Oriented Concepts

- Classes: Blueprints or templates defined at compile time that encapsulate common attributes (nouns representing state) and behaviors (verbs representing operations).
- Objects: Runtime instances of classes created in memory using the
new operator, each possessing its own state. - Abstraction: The process of extracting essential attributes and behaviors from real-world entities to model them programmatically.
Class Components and Methods
- Constructors:
- Share the exact name of the class and do not have a return type.
- Used to instantiate objects and initialize class attributes.
- Classes can define multiple constructors (overloading) provided each constructor has a distinct parameter type list.
- Accessor Methods:
- Non-
void return types used to retrieve attribute values or return the results of computations. - Must include an explicit
return statement.
- Mutator Methods:
- Have a
void return type and modify the internal state or attribute values of an object. - Executed as standalone statements rather than expressions with values.
- Dot Notation: Binary syntax where the left-hand side represents an object reference and the right-hand side represents an attribute or method.
Variable Shadowing and the this Keyword
- Variable Shadowing: Occurs when input parameter names match attribute names, resulting in parameter variables hiding class attributes.
- The
this Reference: Refers to the context object receiving the method or constructor call; used to disambiguate class attributes from input parameters (e.g., this.name = name;).
Reference Types and Memory Model
- Primitive Types vs. Reference Types:
- Primitive Types: Store raw values directly in memory (e.g.,
int at 32-bit, double at 64-bit, boolean, char). Default values include 0, 0.0, and false. - Reference Types: Store the memory address of an object (e.g.,
String, Person, Point, Scanner). Default value is null.
- Null Safety: Invoking a method or field on an uninitialized reference variable that holds
null results in a NullPointerException. - Object Equality (
==):- Applied to primitives: Compares stored values directly.
- Applied to references: Compares object memory addresses rather than attribute values; two distinct objects with identical attributes evaluate to
false under ==.
- Reference Aliasing: Occurs when two or more reference variables store the same memory address, causing mutations performed through one variable to be reflected in the other.
Static Members vs. Instance Members
- Static Variables:
- Declared using the
static keyword and belong to the class rather than to any specific instance. - Shared across all instances of the class, meaning a modification by one instance is visible to all others.
- Can be accessed directly via the class name without creating an object (e.g.,
Student.Count).
- Static Methods:
- Belong to the class and do not require an instance to execute.
- Cannot directly access non-static (instance) attributes or methods because no context object (
this) is bound.
Helper Methods
- Definition: Auxiliary methods, typically declared
private, that assist public methods in executing complex or repetitive routines. - Benefits:
- Eliminate redundant code blocks across multiple class methods.
- Enhance modularity, maintainability, and code readability.