Chapter 11 Flashcards
Static type vs dynamic type
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
overriding
-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.
A super call in a method
- always has the form:
super.method-name( parameters ) - contrary to super calls in constructors, may occur anywhere within that method
- 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
method polymorphism
-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
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.
toString
reference equality
-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
content equality
-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;
}
Declaring a field or a method __ allows direct access to it from (direct or indirect) subclasses
protected
-usually reserved for methods and constructors
-not usually applied to fields- weakens encapsulation
instanceof Operator
-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
Use of the instanceof Operator is often followed immediately by a __ of the object reference to the identified type.
- cast
ie:
ArrayList<MessagePost> messages = new ArrayList<>();
for(Post post : posts) {
if(post instanceof MessagePost) {
messages.add((MessagePost) post);
}
}</MessagePost>