Sign in to save

Bookmark this page so you can find it later.

Sign in to save

Bookmark this page so you can find it later.

Object-Oriented Programming cheat sheet - grade 10-12

Click image to open full size

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. 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. 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. 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. 4 Why is it usually better to make fields private and provide public getter and setter methods instead of making the fields public?

Understanding Object-Oriented Programming

When a program creates an object, a variable usually stores a reference to that object rather than the object itself. Two variables can refer to the same object. This matters because changing the object through one variable can be seen through the other variable.

Students often expect a copy when they assign one object variable to another. A real copy needs separate code, often a copy constructor or a method that creates a new object.

Constructors should establish a valid starting state. For example, a bank account should not begin with a negative balance unless the program deliberately allows overdrafts.

Encapsulation is more than hiding fields from other code. It creates one trusted place for rules. A method that changes a score can reject values below zero or above the maximum.

A method that changes a password can check its length. Without these checks, any part of a large program could put an object into an invalid state. Getter methods need care too.

Returning a mutable list directly may let outside code change private information. In that case, a program may return a safe copy or provide controlled methods for adding and removing items.

Inheritance works best when the child truly is a more specific form of the parent. A Dog is an Animal because it can be used wherever an Animal is expected. A Car is not an Engine, even though a car contains an engine.

That relationship is usually better represented by composition. Composition means one object holds or uses another object. It often makes designs easier to change.

For example, a GameCharacter can contain a Weapon object. The character can then switch weapons without needing a new subclass for every possible weapon choice.

Polymorphism becomes useful when a program handles a collection of related objects. A drawing program might store circles, rectangles, and triangles in one collection of Shape references. The program can call draw on each item without writing separate loops for every shape.

The correct overridden method runs based on the actual object created. Interfaces support a similar idea when classes share an ability but do not belong in one family.

A Printer, a File, and a NetworkConnection might all support a close action through a common interface. Each class supplies its own details.

When reading a design diagram, pay attention to the direction and meaning of every relationship. Inheritance shows an is a relationship. A line showing one class containing another usually shows a has a relationship.

Multiplicity tells how many related objects may exist, such as one teacher linked to many students. In code, test overridden methods through a parent reference, since that is where polymorphism errors often appear.

Watch for accidental method overloading, where a changed parameter list creates a different method instead of replacing the inherited one. Using an override annotation helps the compiler catch that mistake.