Object-Oriented Programming Cheat Sheet
A printable reference covering classes, objects, encapsulation, inheritance, polymorphism, interfaces, constructors, and UML relationships for grades 10-12.
Related Tools
Related Labs
Object-oriented programming organizes programs around objects that store data and perform actions. This cheat sheet helps students remember how classes, objects, methods, and relationships work together in Java-style code. It is useful when designing programs, reading UML diagrams, or preparing for programming assessments. Students need these ideas to build larger programs that are easier to maintain and reuse. The core of OOP is creating classes as blueprints and objects as usable instances of those blueprints. Encapsulation protects data by keeping fields private and providing public methods for controlled access. Inheritance lets one class reuse and extend another class, while polymorphism lets related objects respond to the same method call in different ways. Interfaces, constructors, method overriding, and UML arrows are key tools for writing clear object-oriented designs.
Key Facts
- A class is a blueprint, and an object is an instance created from that class, such as Student s1 = new Student("Maya");.
- Encapsulation means fields are usually private and are accessed or changed through public methods such as getName() and setName(String name).
- A constructor has the same name as the class, has no return type, and runs when an object is created with new.
- Inheritance in Java uses extends, as in class Dog extends Animal, so Dog inherits accessible fields and methods from Animal.
- Method overriding occurs when a subclass defines a method with the same name, return type, and parameters as a superclass method.
- Polymorphism allows a superclass reference to point to a subclass object, such as Animal a = new Dog();, and overridden methods use the object's actual type.
- An interface defines required behaviors, and a class uses implements to promise it will provide those methods, such as class Bike implements Drivable.
- In UML, inheritance is shown with a hollow triangle arrow pointing from the subclass to the superclass.
Vocabulary
- Class
- A class is a blueprint that defines the fields and methods shared by a type of object.
- Object
- An object is a specific instance of a class with its own field values.
- Encapsulation
- Encapsulation is the practice of hiding internal data and controlling access through methods.
- Inheritance
- Inheritance is a relationship where one class gains the accessible features of another class.
- Polymorphism
- Polymorphism allows different related classes to be treated through a common type while using their own method behavior.
- Interface
- An interface is a contract that lists methods a class must implement.
Common Mistakes to Avoid
- Making all fields public is wrong because it breaks encapsulation and lets other code change object data without validation.
- Forgetting to use new when creating an object is wrong because a variable like Student s; only declares a reference and does not construct an object.
- Confusing overloading with overriding is wrong because overloading uses different parameter lists in the same class, while overriding replaces inherited behavior in a subclass.
- Calling private superclass fields directly from a subclass is wrong because private members are only accessible inside the class that declares them.
- Assuming an interface stores complete object behavior is wrong because interfaces mainly define required methods, while implementing classes provide the working code.
Practice Questions
- 1 A class Car has fields make and speed. If you write Car c1 = new Car("Toyota", 40); and Car c2 = new Car("Honda", 55);, how many Car objects are created?
- 2 A BankAccount starts with balance 100. The method deposit(25) adds to balance, and withdraw(40) subtracts from balance. What is the final balance after deposit(25), withdraw(40), and deposit(10)?
- 3 A superclass Shape has method area(). Subclasses Circle and Rectangle both override area(). If Shape s = new Circle(); is used, which class's area() method runs when s.area() is called?
- 4 Why is it usually better to make fields private and provide public getter and setter methods instead of making the fields public?