This week, we're tackling one of the most important concepts in Java and FRC programming: Object-Oriented Programming (OOP). Understanding OOP will allow us to write cleaner, more organized, and more reusable code.
Understand the difference between a class and an object.
Learn how to define a class with fields and methods.
Learn how to create objects (instances) of a class.
Understand and use constructors.
Understand properties, visibility, and encapsulation.
Classes are the building blocks of object-oriented programming. They are a system for organizing and reusing code. Think of a class as a blueprint for an object. It defines the properties and behaviors of the object, but it is not the object itself.
When you create an object from a class, you are creating an instance of that class. The object has its own unique properties and behaviors, but it is based on the blueprint defined by the class.
Defining a Class
To define a class in Java, you use the class keyword followed by the class name. Here is an example of a simple class definition:
public class Person {
public String name = "";
public int age = 0;
}
In this example, we define a class called Person with two properties: name and age. The properties are initialized with default values of an empty string and zero, respectively.
To create an object from a class, you use the new keyword followed by a call to the class's constructor. Here is an example of creating a Person object:
Person person = new Person();
In this example, we create a Person object and assign it to a variable called person. The object is created using the default constructor of the Person class, which does not take any arguments. We will learn more about constructors later in this lesson.
Properties (or fields) are variables that are associated with an object. They define the state of the object and can be accessed and modified from outside the class. In the Person class, name and age are properties. These are unique to each instance of the class. For example, you can create multiple Person objects with different names and ages.
Accessing and Changing Properties
You can access and modify the properties of an object using dot notation. Here is an example of accessing the name property of a Person object:
public class Test {
public static void main(String[] args) {
Person person = new Person();
person.name = "Alice";
System.out.println(person.name); // Output: Alice
person.age = 30;
System.out.println(person.age); // Output: 30
}
}
In this example, we create a Person object, assign "Alice" to the name property and 30 to the age property. We then print the values.
Getters and Setters
In Java, it's a common practice to control access to properties using methods called "getters" and "setters". This concept is called encapsulation. Instead of making fields public, we make them private and provide public methods to get and set their values. This allows us to add logic or validation before a field is read or changed.
Here is an example of defining getters and setters for the name property of the Person class:
public class Person {
private String name = "";
// Getter for name
public String getName() {
return name;
}
// Setter for name
public void setName(String value) {
this.name = value;
}
}
In this example, the getter for the name property converts the name to uppercase before returning it. The setter trims leading and trailing whitespace from the provided value before assigning it. When you call person.getName() or person.setName(" Bob "), these methods are executed.
Property Visibility
By default, properties in Java are "package-private" if no modifier is used. However, you'll almost always declare them as public or private.
public: The property is accessible from anywhere.
private: The property is accessible only from within the class itself.
If you want a property to be accessible from outside the class, you can make it public. If you want it to be accessible only from within the class (and often managed by getters and setters), you make it private.
Here is an example of a private property in the Person class:
public class Person {
private String ssn = "";
}
public class Test {
public static void main(String[] args) {
Person person = new Person();
// person.ssn = "123-45-6789"; // Error: 'ssn' has private access in 'Person'
}
}
In this example, ssn is a private property. If you try to access it from outside the Person class, you will get a compilation error.
Methods
Methods are functions that are defined inside a class. They are used to define the behavior of an object. In Java, methods are defined with a return type, a name, and a list of parameters.
public class Calculator {
public int add(int a, int b) {
return a + b;
}
}
public class Test {
public static void main(String[] args) {
Calculator calculator = new Calculator();
int sum = calculator.add(5, 10);
System.out.println(sum); // Output: 15
}
}
In this example, we define a class called Calculator with a method called add. The add method takes two int parameters and returns their sum.
Constructors
Constructors are special methods that are called when an object is created. They are used to initialize the object's fields. In Java, a constructor has the same name as the class and no return type.
Let's add a constructor to our Person class:
public class Person {
public String name;
public int age;
// Constructor
public Person(String name, int age) {
this.name = name;
this.age = age;
System.out.println("Name: " + name);
System.out.println("Age: " + age);
}
}
public class Test {
public static void main(String[] args) {
Person person = new Person("Alice", 30); // Output: Name: Alice
// Age: 30
System.out.println(person.name); // Output: Alice
System.out.println(person.age); // Output: 30
}
}
In this example, we create a Person object with the name "Alice" and age 30, which are passed to the constructor. The constructor initializes the name and age fields.
Constructor Overloading
You can create multiple constructors with different parameters for flexibility.
public class Person {
public String name;
public int age;
public Person() {
this("", 0);
}
public Person(String name) {
this(name, 0);
}
public Person(String name, int age) {
this.name = name;
this.age = age;
}
}
Next week, we'll start to see how these Java concepts apply to FRC. We'll have separate paths for rookies and returning members to start building the skills they'll need for the season.