type
A class is a "____"
Data and methods
What are the two main attributes of a class?
Instance data
Values that are within the class but not within the methods; generally placed at the top of the class
copy
Each object that gets instantiated for the class gets its own ____ of the instance variables
Methods
functions/procedures (behaviors) within the class
body
public double meth1(int var1, int var2) {
double total;
total = var1 * var2;
return total;
}
//What is the content of the method referred to as?
Return statement
control immediately goes back to call
return;
What is the return statement for a void method?
Local variables
Variables declared inside a method, and are only accessible in that method. In other words, the scope of these variables is the method in which they were created.
Parameters
Variables passed into a method
Formal paramaters
Names of the parameters in the header declaration
Actual paramaters
Values passed in when running
T
(T/F) Arrays can be parameters
Static methods
Methods that are not called in the context of an object.
Non-static methods
Methods that are always called in the context of an object
this
A Java reserved word that is used inside methods. It refers to the object upon which the method was invoked.
Class interface
A group of specified certain methods for outsiders to use
Visibility modifiers
Modifiers that control access
public
Modifier that allows anyone to access instance data and methods
private
Modifier that only allows the class itself to access instance data and methods (i.e. objects of that class)
protected
Modifier that allows a subclass to access instance data and methods from its super class, vice versa, and classes within the same package
Constructor
A special method that is automatically called when objects are instantiated.
initialize
A constructor's primary use is to _______ instance variables.
Scope
The region of a program where variables are accessible.
Overloading
The use of the same name with different paramater lists (types) to create multiple versions of a method
Method signature
The part of a method declaration that includes the name, along with the paramater type and order.
Example:
meth1(int var1, int var2)
F
(T/F) A method signature includes the return type of that method
T
(T/F) Constructors can be overloaded
this(v1, v2, v3);
What is the proper syntax for a chained constructor call? (use 3 variables, denoting them as v1, v2, v3)
F
(T/F) A constructor must always have parameters as input
B
If a constructor for a class is not written, Java provides one for you when you create an object of that class. However, if you provide one with pamaraters, and you create an object with no paramaters, what happens?
A. The object is created with default values, and the code compiles.
B. The compiler will throw an exception.
A
int a;
What is this an example of?
A. Declaration
B. Initialization
C. Instantiation
A
String s;
What is this an example of?
A. Declaration
B. Initialization
C. Instantiation
B
int a;
a = 0; //What does this line represent?
A. Declaration
B. Initialization
C. Instantiation
C
String s;
s = new String(“Hello”); //What does this line represent?
A. Declaration
B. Initialization
C. Instantiation
equals()
If we want to check whether two objects contain the same values, what method or operator do we use (hint: primarily used for objects)?
==
If we want to check whether two objects reference the same data, what method or operator do we use (hint: primarily used for primitive types)?
toString()
The method invoked when an attempt to print an object of that type occurs
static
A modifier that creates one copy of a variable or method that is shared by all instances of the class
In the class
Where is the memory space for static variables located?
F
(T/F) Static methods can reference instance variables
null
A reserved word for objects that have no reference; can occur after declaration but no instantiation
Copy constructor
A special type of constructor that takes in an object of that class and "copies" its contents. When called, an object with the same values as the paramater object will be created.
Wrapper classes
A class that corresponds to a primitive type. Allows the usage of primitive types as objects. i.e. Integer, Double, etc.
T
(T/F) Java will automatically convert between primitive and Wrapper when needed
Auto-boxing
Converting a wrapper to a primitive type
Unboxing
Converting a primitive type to a Wrapper
Javadoc
Documenting your code
Inheritance
The process of deriving a new class from an existing one. Automatically contains some or all of the methods of the original. Can add new ones to it.
extends
A keyword that denotes that a class is a subclass of a super class; placed in class header
is-a
What type of hierarchy represents the super-sub class relationship?
super
keyword for denoting access to parent's members
super(v1);
What is the proper syntax for calling the constructor of the parent class from the child class? Use "v1" as the variable.
F
(T/F) When using the super() call in the constructor of the child class, this line of code can appear anyhwere within the body of the constructor.
T
(T/F) If the first line of a sub-class constructor is not an explicit call to a super class constructor, Java inserts super() automatically.
Overriding
When a child class defines a method with the same name (and signature) as the parent, child's version overrides the parent's version
@Override
What is the correct syntax for notifying the compiler of a method override for a child class?
F
(T/F) Static methods can be inherited and overridden
single
Java has _____ inheritance; it can extend at most one class
Object
The class that all classes are ultimately derived from. Contains methods such as equals(), toString(), clone().
instanceof
keyword that checks if an object is an instance of another class
getClass()
Method that returns the class an object is an instance of
this(xxxx);
chained constructor call (use xxxx as param, variable, or method name)
this.xxxx;
Instance data of object that is being called upon.
(use xxxx as param, variable, or method name)
this.xxxx();
Calling a method within the class, do not necessarily need "this"
(use xxxx as param, variable, or method name)
super(xxxx);
calling super class constructor
(use xxxx as param, variable, or method name)
super.xxxx;
Accessing instance variable from super class. (use xxxx as param, variable, or method name)
Abstract
a “placeholder;” useful for super and sub classes in which the super class contains a abstract method that must be included in the subclasses.
F
(T/F) Abstract classes cannot contain non-abstract methods
T
(T/F) In order for an abstract method to exist, the subclasses must either override the method or be absract themselves.
can
Abstract methods (can/can’t) have concrete constructors.
F
(T/F) You can instantiate an abstract class with new()
final
A keyword that is used to denote that a class cannot be extended, the value of a variable cannot be changed, or that a method cannot be overridden.
Static type
A variable’s type at compile time; what class the object is declared as
Person
Suppose a Person class with a constructor and methods is created. Also suppose that a subclass named Student is created and extends Person. Now, suppose we create a Person object p and write the following lines of code.
Person p;
p = new Student(“Mary”, 1);
//What is the static type of p?
Dynamic type
the type of object a variable references at run-time; the class an object is instantiated as.
Dentist
Suppose a Person class with a constructor and methods is created. Also suppose that a subclass named Dentist is created and extends Person. Now, suppose we create a Person object p and write the following lines of code (assume “xxxx” are paramaters).
Person p;
p = new Dentist(xxxx);
//What is the dynamic type of p?
F
(T/F): If you do not write a toString() method for a class and attempt to print an object that is an instance of that class, you will get a compiler error.
public abstract void method1()
Write the method declaration of an abstract void method that takes no parameters, is public, and is named “method1”.
Polymorphism
To have many forms. This allows us to deal with diverse (but related) members of a group in a general way.
yes
Suppose a class Animal is defined, and Bird is another defined class that is a subclass of Animal. Are the following lines of code legal? (yes/no)
Animal a1;
a1 = new Bird();
no
Suppose a class Animal is defined, and Bird is another defined class that is a subclass of Animal. Are the following lines of code legal? (yes/no)
Animal a1;
Bird b1 = new Animal();
T
(T/F) We can assign or subsittute a member of a subclass for a memer of a parent class, but not vice versa
Dynamic binding
Binding of which type of object is “in there” and influences the call is is deferred until run-time; Compiler simply insures either way is legal.
dynamic
Suppose obj is an instantiated object of some subclass, and meth() is a method that exists in both the parent and super class. The method that was invoked on a obj.meth() call is determined by the ____ run-time type of obj.
lowest
In a chain of multiple subclasses, whatver class is ____ in hierarchy determines which methods are called.
F
(T/F) You cannot cast an object down the hierarchy (casting parent to a child)
T
(T/F) It is possible for valid casts to fail at run-time.
Generics
Classes and method definitions with types. You write a _____ class/method, then it can be created to handle any different type of object.
ArrayList
Like an array, like a list (can grow in size).
import java.util.ArrayList;
Write the line of code that imports an ArrayList for use.
ArrayList<Type> list = new ArrayList<>();
How do you correctly declare and instantiate an ArrayList? Use “Type” and “list” in your answer when appropriate.
to the right
When you add an object to an ArrayList at a particular index, which way does the rest of the list shift?
Interfaces
Specification of a set of abstract behaviors (methods). Classes choose to provide these behaviors and then advertise themselves as doing so.
F
(T/F) A class does not have to implement every method in the interface it implements.
T
(T/F) A class can have other methods besides the ones it implements from an interface.
F
(T/F) An interface is a class.
T
(T/F) Interfaces can be in hierarchies
Recursion
One made to same method as resident in; The process of calling a method within the method itself.
F
(T/F) Recursive calls are essentially “calling yourself”