CLASSES AND OBJECTS Flashcards
What code is found within the class body?
constructors for initializing new objects, declarations for the fields that provide the state of the class and its objects methods to implement the behavior of the class and its objects.
What extra information can be provided at the declaration of the class?
Modifiers such as public, private, and a number of others that you will encounter later. The class keyword The class name, with the initial letter capitalized by convention. The name of the class's parent (superclass), if any, preceded by the keyword extends. A class can only extend (subclass) one parent. A comma-separated list of interfaces implemented by the class, if any, preceded by the keyword implements. A class can implement more than one interface. The class body, surrounded by braces, {}.
EG:
class MyClass extends MySuperClass implements YourInterface { // field, constructor, and // method declarations } • In this scenario, MyClass is a subclass of MySuperClass and implements the YourInterface interface.
What are modifiers? And give examples
• A modifier is a keyword placed in a class, method or
variable declaration that changes how it operates and
to what extent it can be accessed.
- abstract
- final
- native
- private
- protected
- public
- static
- strictfp
- synchronized
- transient
- volatile.
What are access modifiers?
• The first (left-most) modifier used lets one control what other classes have access to a member field.
public modifier—the field is accessible from all classes.
private modifier—the field is accessible only within its own class.
What is a method?
A method is a set of code which is referred to by name and can be called (invoked) at any point in a program simply by utilizing the method’s name.
What is the structure of a method declaration and method signature and give an example?
- Modifiers—such as public, private, and others you will learn about later.
- The return type—the data type of the value returned by the method, or void if the method does not return a value.
- The method name—the rules for field names apply to method names as well, but the convention is a little different.
- The parameter list in parenthesis—a comma-delimited list of input parameters, preceded by their data types, enclosed by parentheses, (). If there are no parameters, you must use empty parentheses.
- An exception list
- The method body, enclosed between braces—the method’s code, including the declaration of local variables, goes here.
• An example of a typical method declaration is as follows: public double calculateRectArea(float area, float length, float width) { //calculation comes here } • Method signature: This comprises of the two components of a method declaration:—the method's name and the parameter types, e.g. in the above case: calculateRectArea(float, float, float)
What are the guidelines for method naming? And give examples
• By convention, method names should be a verb in
lowercase or a multi-word name that begins with
a verb in lowercase, followed by adjectives, nouns, etc.
• In multi-word names, the first letter of each of the
second and following words should be capitalized
- Example of method names would include:
- add
- addIntegers
- getImage
- getImageData
- compareWith
- setY
- isNull