Programming II Flashcards
What does new do?
new invokes Java’s mechanism for creating new objects.
new does four things:
- Reserves memory in the heap for the object
- Creates an object there using the class as a blueprint
- Runs a constructor method defined within the class
- Returns a reference to the object
How the user the Scanner
instance variables
variables inside a class but outside a method
constructors
- a special method that is used only to create an instance of a class
- initialise variables as soon as you create an object
3 characterstics of a class
- is a compile-time entity: created at development time, not at runtime
- is a blueprint from which objects are instantiated
- defines a collection of attributes (fields) and methods that all its instantiations will have
4 characterstics of an object
- is a run-time entity: it must be created from a class during program execution
- is an instance of a class
- stores a value for each field
- methods can be invoked on an object
identify each element of the following code:
int argsNum =
- “argsNum” is a variable
- “int” tells us the variable is of type int
- = assignment operator - takes whatever is on the right side and copies that value to the left side variable
encapsulation
The object maintains a boundary between its state and behaviour and the outside world.
Access modifiers enfore encapsulation:
- public - all classes have access to the element
- private - only methods inside the class have access
- protected - related to inheritance
- package - default, anyone from within package
difference between classes and objects and instances
A class is a blueprint which you use to create objects.
An object is an instance of a class - it’s a concrete ‘thing’ that you made using a specific class. So, ‘object’ and ‘instance’ are the same thing, but the word ‘instance’ indicates the relationship of an object to its class.
For example, suppose you have a class House. Your own house is an object and is an instance of class House. Your sister’s house is another object (another instance of class House).
how to create an object which is an instance of the class “House”
House myHouse = new House();
fields vs. attributes
An attribute is another term for a field. It’s typically a public constant or a public variable that can be accessed directly.
A class stores its state in fields.
A class aggregates attributes (fields) and methods (functions).
property
the getter and setter combination
fields vs. variables
Example of a inheritance use case
Suppose that you’re writing a human-resources application and want to use the Person class as the basis (also called the super class) for a new class called Employee. Being the child of Person, Employee would have all of the attributes of a Person class (Weight, Hair, Height), along with additional ones, such as:
- Taxpayer identification number
- Employee number
- Salary
import statements
An import statement tells the Java compiler where to find classes that you reference inside of your code.
import ClassNameToImport;
extra:
The class name should be fully qualified, meaning that it should include its package.
To import all classes within a package, you can put .* after the package name. For example, this statement imports every class in the com.makotojava package
import com.makotojava.*;
Importing an entire package can make your code less readable, however, so I recommend that you import only the classes that you need, using their fully qualified names.
How does a class declaration look like?
In general, class declarations can include these components, in order of when to write them:
- Modifiers such as public, private, and a number of others that you will encounter later.
- 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, {}.
Classes can have two types of members…
variables and methods.
The values of a class’s variables distinguish each instance of that class and define its state. These values are often referred to as instance variables.
components of variables
A variable has:
- An accessSpecifier
- A dataType (primitive or other…)
- A variableName
- Optionally, an initialValue
The possible accessSpecifier values for variables are…
No specifier (also called friendly or package private access): Only objects whose classes are defined in the same package can see the variable.
private: Only the class containing the variable can see it.
public: Any object in any package can see the variable. (don’t ever use this value; see the public variables sidebar.)
protected: Any object defined in the same package, or a subclass (defined in any package), can see the variable.
two main categories of methods
- constructors
- all other methods, which come in many types
stack vs. heap
both reside on the memory
ADD MORE
nested if (else) statements
else if Statements
conditional operators
like using if else statments, but more compact:
- first part is the test: age > 50
- making it boolean using: ?
- if it’s true, execute first option
- seperate with :
- if it’s wrong, execute second option
for loop
cosists of three elements:
- where you want the loop to start
- where you want the loop to end (the condition)
- how much you want it to increment it by