Chapter 3. Methods and encapsulation

Exam objectives covered in this chapter

What you need to know

[1.1] Define the scope of variables. Variables can have multiple scopes: class, instance, method, and local. Accessibility of a variable in a given scope.
[2.3] Know how to read or write to object fields. Object fields can be read from and written to by directly accessing instance variables and calling methods. The correct notation to call methods on an object. Methods may or may not change the value of instance variables. Access modifiers affect access to instance variables and methods that can be called using a reference variable. Nonstatic methods can’t be called on uninitialized objects.
[2.4] Explain an Object's Lifecycle (creation, “dereference by reassignment” and garbage collection). Differences between when an object is declared, initialized, accessible, and eligible to be collected by Java’s garbage collection. Garbage collection in Java.
[6.1] Create methods with arguments and return values; including overloaded methods. Creation of methods with correct return types and method argument lists. Creation of methods with the same names, but a different set of argument lists.
[6.3] Create and overload constructors; including impact on default constructors. Like regular methods, constructors can be overloaded. A default constructor isn’t the same as a no-argument constructor. Java defines a no-argument constructor when no user-defined constructors are created. User-defined constructors can be overloaded.
[6.5] Apply encapsulation principles to a class. Need for and benefits of encapsulation. Definition of classes that correctly implement the encapsulation principle.
[6.6] Determine the effect upon object references and primitive values when they are passed into methods that change the values. Object references and primitives are treated in a different manner when passed to methods. Unlike reference variables, the values of primitives are never changed in the calling method when they’re passed to methods.

Look around, and you’ll find multiple examples of well-encapsulated objects. For instance, most of us use the services of a bank, which applies a set of well-defined processes that enable us to secure our money and valuables (a bank vault). The bank may require input from us to execute some of its processes, such as depositing money into our accounts. But the bank may or may not inform us about the results of other processes; for example, it may inform us about an account balance after a transaction, but it likely won’t inform us about its recruitment plans for new employees.

In Java, you can compare a bank to a well-encapsulated class and the bank processes to Java methods. In this analogy, your money and valuables are like object fields in Java. You can also compare inputs that a bank process requires to Java’s method parameters and compare the bank process result to a Java method’s return value. Finally, you can compare the set of steps that a bank executes when it opens a bank account to constructors in Java.

In the exam, you must answer questions about methods and encapsulation. This chapter will help you get the correct answers by covering the following:

  • Defining the scope of variables
  • Explaining an object’s life cycle
  • Creating methods with primitive and object arguments and return values
  • Creating overloaded methods and constructors
  • Reading and writing to object fields
  • Calling methods on objects
  • Applying encapsulation principles to a class

Let’s get started with the scope of variables.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.157.197