Chapter 4 Flashcards
What does an object have?
An object has state and behaviour.
What does a class contain?
A class contains data declarations and method declarations.
What's the difference between a method declaration called public int roll() and public static int roll() ?
Without putting static, the method will compute for the object on which the object is called. The variables present in the class of the called object will be the ones of the object on which the object was called.
When writing a static method, it means that the method refers to the class and the variables called inside the method will affect every object of that class.
What are accessors and mutators?
These are also known as getters and setters. Respectively, they retrieve the value of a specific variable and set a new variable to it.
Why is it good practice to define a toString method for a class?
The toString method will return a string of characters representing the object in the way we want it to.
What is the data scope?
The scope of data is the area in a program in which that data can be referenced.
If data is declared at the class level, then it can be referenced by all methods in that class. If data is declared within a method, it can be used only in that method.
What is instance data?
Instance data is any variable declared at the class level. These variables are not created with the creation of the class itself, but with each creation of the object.
What is UML?
UML stands for Unified Modeling Language. Its diagrams show relationships among classes and objects.
A UML class diagram consists of one or more classes, each with sections for the class name, attributes (data), and operations (methods).
What are the two possible views of an object? What is encapsulation?
An object can be seen in two different ways:
- Internally: the details of the variables and methods of the class that defines it
- Externally: the services that an object provides and how the object interacts with the rest of the system.
Encapsulation consists in hiding the inner workings of an object from the client. This is done to avoid the client to directly access or modify the data of an object. With encapsulation the only interaction possible is via the methods of a given class (that we define).
What is the purpose of a modifier? What does a visibility modifier do? What modifiers do we have?
A modifier is a Java reserved word that specifies particular characteristics of a method or data.
A visibility modifier allows encapsulation to be done at different degrees.
With "public", the method or data could be accessed from everywhere. With private, the method or data could be accessed only inside the class in which it's declared. Without writing anything, the method or data could be accessed only from inside the package (default visibility).
Why can’t constants be declared private?
Because although the user can access the constant, it cannot be modified.
What is the difference between service methods and support methods?
Service methods are public, they’re made to interact with the user.
Support methods are private, they’re made to assist service methods and shouldn’t therefore be accessed directly by the user.
What does happen to the flow of control when a method is invoked?
The flow of control jumps to the method and executes its code (from top to bottom), before returning to the original flow.
What does a method header have?
The method header has (for example):
public char calc (int num1, int num2, String message)
- public is the visibility modifier
- char is the return type of the metjhod
- calc is the name of the method
- inside the parenthesis we find the parameters, each indicating variable type and its name)
What does a method body have?
A method body contains the operation with the variables given in the header. The return expression (unless the methods has return type void) must be consistent with the declaration.