Java Questions 21 - 30  «Prev  Next»

Java Questions 27

  1. How should you write your Java classes?
    Answer:
    You have to write your classes and code in a way that supports flexibility and maintainability.
  2. What is a key benefit of encapsulation?
    Answer:
    Encapsulation is the ability to make changes in your implementation code without breaking the code of others who use your code.
  3. How can you think of encapsulation?
    Answer:
    You want to hide implementation details behind a public programming interface.
  4. What is meant by public programming interface?
    Answer:
    By interface we mean the set of accessible methods your code makes available for other code to call. This is also known as your coders API.
  5. What must you do to ensure your design includes encapsulation?
    Answer:
    1. Keep instance variables protected by means of an access modifier
    2. Calling code should be forced to use public accessor methods
    3. For methods, use the JavaBeans naming convention of a) set <someProperty> b) get <someProperty>

  6. What does encapsulation force a developer to do?
    Answer:
    Encapsulation forces callers of our code to go through methods rather than accessing variables directly. If you want to change the value of a variable use a setter method.
  7. What is the purpose of getter and setter variables?
    Answer:
    Getters and Setters are methods that other programmers must go through in order to access your instance variables.
  8. When you design object oriented code, what should you keep in mind?
    Answer:
    Good OO design dictates that you plan for the future.
  9. What is the correct way to access variables in your Class?
    Answer:
    The programmer should force calling code to go through methods rather than going directly to the instance variables. [P89 SCJP by K.S.]
  10. What are the syntax rules for identifiers?
    Answer:
    Identifiers can begin with a letter, an underscore, or a currency character.
    Examples:
    int numdays;
    int _numdays;
    int $numdays;