Implementing Classes Examples

version 1. basics

version 2. adding a constructor

version 3. adding toString()

version 4. adding move()

We add an instance method called move which changes the Point's x and y coordinate.

version 5. private

Now the only way we can change a Point's x and y is through move, but we've introduced some errors.

version 6. addition of accessor methods

Because x and y are private, we can't access them directly, but now we can obtain read-only access to them through the accessor methods getX() and getY(). Notice that both x and y are private, but getX() and getY() are public.

version 7. mutator methods

x and y can now be changed, but only indirectly through the public methods setX() and setY().

version 8. arrays of points

There's nothing different about our Point class here. We just look at the syntax for creating and using arrays of points.

version 9. comparing the contents of two Points

version 10. making a copy of a Point

version 11. overloading the constructor

version 12. composition

Building a class comprised of other classes.