Guide · Examples · Object-oriented programming

Object-Oriented Programming – Example Tasks (Java)

Tasks on object-oriented programming, arranged from the first classes with attributes, through constructors and encapsulation, to arrays of objects, inheritance, polymorphism and interfaces. Every task has a hint and a solution in Java.

Meant for students who have mastered the basics (branching, loops, arrays) and are learning Java – at school, in a club or on their own.

  • Secondary school and advanced primary school students
  • Java
  • Tasks with solutions
How to work: for every task first draw the class on paper (name, attributes, methods), and only then write the code. Open the solution when your program works for the example from the task.

Some lessons linked below are available only in Serbian for now. They are marked (SR).

1 First classes and objects

Attributes, objects created with new and the first methods. Start here.

2 Constructors and encapsulation

An object sets and protects its own data: the constructor, private attributes, getters and setters, toString().

3 More objects: an array and ArrayList

Objects in an array or a list: the best one, the average, searching, sorting and removing.

4 Inheritance, polymorphism and interfaces

Several classes with common properties: extends, super, @Override, abstract classes, interface and Comparable.

5 Check your knowledge and continue

When you solve the tasks, do the test, and go back to the lessons for the theory.

Suggested order

  1. Classes and objects – basic tasks (tasks 1–13) (SR)
  2. Additional examples: classes and objects
  3. Constructors, getters and setters (then tasks 14 and 15 from the first page) (SR)
  4. An array of objects and ArrayList (SR)
  5. Inheritance, polymorphism and interfaces (SR)
  6. Test (SR)
Note: the tasks are in Java. Object-oriented programming in C++ (classes, constructors) has the same meaning – the ideas of the solutions carry over, only the syntax changes.

Useful external resources