A7 - Classes
Classes Introduction
Definition: A class is a blueprint for what an Object stores and how it behaves.
Purpose: Defines the type of data an Object will store and its methods.
Key Concept: Encapsulation - combining multiple data into a single class.
Classes Overview
Files
Each class written in its own file.
File name must match the class name.
Example: A class named
Personshould be in a file calledPerson.java.
Parts of a Class
Attributes: Store data for objects (e.g., first name, last name).
Constructors: Special methods for building objects and assigning initial values to attributes.
Methods: Perform actions on attributes.
Attributes
Attributes are not mandatory but are typically included.
Access Levels: Determines visibility of attributes/methods:
public: Accessible outside the class.
private: Not accessible outside the class.
protected: Accessible by subclasses or classes in the same package.
Modifiers:
final: Value cannot be changed after initialization.
static: Shared among all instances (class attribute).
Format for Attributes:
accessLevel modifiers type attributeName;Examples:
public static final double PI = 3.14;private String name;private int age = 0;
Constructors
Rules for creating constructors:
Must be
public.Cannot have a return type.
Can receive parameters.
Can be overloaded.
A default constructor is one that receives no parameters; it initializes attributes to default values.
Format for Constructor:
public className(parameters) { //Code }
Methods
Getters
Allow public access to private attributes.
Format for Getter:
public returnType getVariableName() { return variableName; }Example:
public int getAge() { return age; }
Setters
Allow changing private attributes publicly.
Format for Setter:
public void setVariableName(variableType variableName) { this.variableName = variableName; }Example:
public void setAge(int age) { this.age = age; }
Other Methods
Classes can have various methods beyond accessors/mutators.
Format for Methods:
accessLevel modifiers returnType methodName(parameters) { //code }
Working with Instances
Creating an Instance:
className variableName = new className(parameters);Accessing Public Attributes:
variableName.attributeNameChanging Public Attributes:
variableName.attributeName = newValueAccessing Private Attributes:
variableName.accessor()Changing Private Attributes:
variableName.mutator(newValue)Accessing Public Class Methods:
variableName.methodName(parameters)
Class Attributes & Methods
Accessing class attributes and methods can be done with or without instances.
Accessing Class Attributes:
className.attributeNameChanging Class Attributes:
className.attributeName = newValueAccessing Class Methods:
className.methodName(parameters)
Keywords
this
Keyword for accessing the current instance within a class.
Usage:
this.attributeNameTo call another constructor:
this(parameter);
null
Represents a variable that does not reference any Object.
Built-in Class Methods
equals
Tests equality of two Objects (returns true or false).
By default compares memory addresses:
Example scenario demonstrating default behavior.
Fixing Equals Method
Custom implementation of equals required:
Format:
public boolean equals(Object o) { // Logic for comparison }Example provided for comparing object attributes.
toString
Returns a String representation of an Object.
Default implementation provides type and memory address.
Fixing toString Method
Custom implementation of toString required:
Format:
public String toString() { // Create and return a text version }
Memory Addresses
Variables store memory addresses, not object data directly.
==operator compares memory addresses instead of actual data values.
Reference Revisited
Objects are passed by reference to methods.
Modifications to a parameter affect the original object because of shared memory address.
Handling null & Exception Prevention
Example scenarios showing handling of null to avoid exceptions.
Package
Group of related classes with unrestricted access to each other’s protected methods/attributes.
Benefits of using packages.
Naming in Packages
Naming conventions for package:
Should be in lowercase.
Avoid starting with java or javax.
Use periods for separation.
Example Package Declaration:
package game.graphic;
Terms and Definitions
Access Level: Visibility level for class data/methods.
Attributes: Data storage in class.
Class: A blueprint for objects.
Class Attribute: Shared across all instances.
Class Method: Access class attributes only.
Constructors: Special methods for constructing objects.
Default Constructor: Automatically provided if none created.
Encapsulation: Combining data into a class.
equals: Method for equality check.
Getter: Method for accessing private attributes.
Information Hiding: Preventing unwanted data changes.
Instance Method: Can access instance and class variables.
Modifiers: Set properties for methods/attributes.
null: Denotes no object reference.
package: Group of related classes.
protected: Access level for subclasses/same package.
public: Fully accessible data.
Setter: Method for altering private attributes.
Static: Class attribute/method.
this: Refers to current instance.
toString: Method for returning a String version of an object.