Chapter 11 Flashcards

1
Q

Static type vs dynamic type

A

Static - Type declared in the source code-the static representation of the program
dynamic - type of object stored in a variable because it depends on assignments at runtime-the dynamic behavior of the program
ie:
Vehicle v1 = new Car();
static type of v1 is Vehicle
dynamic type of v1 is Car

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

overriding

A

-a situation where a method is defined in a superclass and a method with exactly the same signature is defined in the subclass.
-@Override may be added before the version in the subclass to make it clear that a new version of an inherited method is being defined.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

A super call in a method

A
  1. always has the form:
    super.method-name( parameters )
  2. contrary to super calls in constructors, may occur anywhere within that method
  3. again contrary to super calls in constructors, no automatic super call is generated and no super call is required; optional - so default behavior gives the effect of a subclass method completely hiding the superclass version of the same method
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

method polymorphism

A

-method calls are polymorphic
-the same method call may at different times invoke different methods, depending on the dynamic type of the variable used to make that call

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Every object in Java has a __ method that can be used to return a string representation of itself. Typically, to make it useful, an object should override this method.

A

toString

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

reference equality

A

-tested for using the == operator.
-determines whether there is one object referred to by 2 different variables or 2 distinct objects
-returns true if var1 and var2 are referring to the same object or false if they are referring to anything else
var1 == var 2

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

content equality

A

-asks whether 2 objects are the same internally - are the internal states of 2 objects the same?
-test whether the values of their 2 sets of fields are equal
-a test of the fields only makes sense if we are comparing fields of the same type
ie:
public boolean equals(Object obj)
{
if(this == obj) {
return true; // Reference equality.
}
if(!(obj instanceof Student)) {
return false; // Not the same type.
}
// Gain access to the other student’s fields.
Student other = (Student) obj;
return name.equals(other.name) &&
id.equals(other.id) &&
credits == other.credits;
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Declaring a field or a method __ allows direct access to it from (direct or indirect) subclasses

A

protected
-usually reserved for methods and constructors
-not usually applied to fields- weakens encapsulation

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

instanceof Operator

A

-tests whether a given object is, directly or indirectly, an instance of a given class
ie:
obj instanceof MyClass

returns true if the dynamic type of obj is MyClass or any subclass of MyClass
-left operand is always an object reference, right operand is always the class name

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Use of the instanceof Operator is often followed immediately by a __ of the object reference to the identified type.

A
  • cast
    ie:
    ArrayList<MessagePost> messages = new ArrayList<>();
    for(Post post : posts) {
    if(post instanceof MessagePost) {
    messages.add((MessagePost) post);
    }
    }</MessagePost>
How well did you know this?
1
Not at all
2
3
4
5
Perfectly