Got a YouTube account?

New: enable viewer-created translations and captions on your YouTube channel!

English subtitles

← Properties - Intro to Java Programming

Get Embed Code
4 Languages

Showing Revision 4 created 05/24/2016 by Udacity Robot.

  1. This class has three instance variables. The number of seats, the driver name,
  2. and the list of passengers. For which are these should we have getters and
  3. setters. The number of seats certainly shouldn't have a setter. You can't
  4. really change the number of seats in a car. Should it have a getter? if you
  5. added one, it's not a mistake. but there's probably no grade reason for having
  6. it. I didn't add one. Now, the driver name definitely needs a setter, because,
  7. when you look through the methods, there is no way of setting it otherwise. So,
  8. let's add a setter. Here is my setter. Do I need a getter? I think I do.
  9. There's no other way of finding out who's driving this car. None of the other
  10. methods make any use of the driver name. I'm assuming it was important since
  11. someone added the instance variable. So, let's add the getter as well. Here is
  12. my getter. And as is so often, the setter and the getter have very simple
  13. implementation. What about the third instance variable? You definitely don't
  14. want to have getters and setters for this variable. It would be very odd to
  15. have a method that holds and places an internal array of the class. And you
  16. also wouldn't really want to return it, because then you would give the user of
  17. the class the possibility of changing its content. So there is a rule of thumb
  18. that says that in general, a class that does not want to return any internal
  19. parts of its implementation that a third party could modify. Now, you've seen a
  20. bunch of useful patterns that hopefully will help you designing the
  21. implementation of a class. I will put those to good use when we implement our
  22. car share application. But before we do that, there is another technical topic
  23. that we need to tackle. Namely, variables and methods that don't belong to
  24. objects, but to the class as a whole.