Objects & Classes (OOP)
AP Computer Science A — object-oriented programming in Java: classes, objects, instance variables, constructors and methods. Worked example and FRQ tip.
AP Computer Science A is built around object-oriented programming (OOP). A class is a blueprint; an object is a specific thing created from that blueprint.
Defining a class
public class Student {
private String name; // instance variable
private int grade;
public Student(String n, int g) { // constructor
name = n;
grade = g;
}
public int getGrade() { // method
return grade;
}
}
Creating and using objects
Student s = new Student("Ayşe", 11);
System.out.println(s.getGrade()); // 11
newcalls the constructor and builds the object.privateinstance variables are hidden — this is encapsulation. Outside code uses getter/setter methods instead of touching the data directly.
Worked example
What does s.getGrade() return above? It returns 11, the value passed to the constructor and stored in grade.
Exam tip
The CSA free-response section almost always asks you to write a class or a method. Practise the pattern: private instance variables, a constructor that sets them, and methods that use this/getters. Know the difference between an object reference and the object itself — two references can point to the same object, so changing one changes both.
Kısa Ders Videosu
Deneme Sınavı
Alıştırma Testi
Kendini dene: anında sonuç ve açıklama.
1. Which Java keyword creates a new object?
2. Making instance variables private and exposing them through getters/setters is an example of:
3. A class is best described as:
Bu konuda desteğe mi ihtiyacın var?
30 dakikalık ücretsiz tanışma görüşmesinde seviye belirlenir, kişiye özel bir çalışma planı kurulur.
Ücretsiz tanışma görüşmesi