Chapter 11 - More about inheritance Flashcards
This takes place when a variable is polymorphic and we plan to call a method on the object that it will hold.
When does
dynamic method lookup take place?
when overiding equals()
When should getClass() be used over instanceof?
If we want to avoid the behavior of a superclass taking into account that subclasses may be passed in, then getClass() is preferred.
These are used by data structures such as hashsets and hashmaps, which use an underlying data structure called a hash table.
These are used to determine which bucket of the hash table an element should go into, and whether that bucket already contains an equal element.
Hashsets and hashmaps rely on both the hashcode() and equals() methods to work correctly.
Why do we need hashcodes in Java?
What is
method polymorphism?
also known as polymorphic method dispatch is the concept that describes how method calls in Java are polymorphic, meaning that the same method call may call different method implementations depending on the dynamic type held by a variable at runtime
what does
Subtyping and substitution
allow
these allow a variable to hold objects of different types. For example, a variable of type Vehicle can hold an object of type Car or Van.
We should use immutable objects as keys in hash-based structures because if an object that is a key changes state, then accessing the same bucket with the changed state of the key may not find the same bucket where the key’s value was originally stored.
Notes:
1. A simple solution is to use immutable objects such as strings as keys.
2. If we cannot adhere to the rule of using immutable objects as keys, it is important to ensure that the state of the key object does not change while being used as a key.
Why should we use immutable objects as keys in hash-based structures?
What 2 things are important to keep in mind when using
getClass()?
when using this 2 things to keep in mind are:
- It will invoke a null pointer exception if a variable has not been initialized and is null.
- It takes into account only the current runtime object and nothing else.
reasons include:
1. can quickly compute a bucket, which leads to fewer operations when searching through elements of a bucket.
2. ensure a fairly even distribution of hashcodes, which spreads out elements among the buckets, again leading to less searching of elements within a bucket.
What are 2 reasons for the
efficiency of hash tables?
What is a super call in methods?
This in methods allows a subclass method to call a method of its superclass
this is sometimes necessary in a situation where a superclass method has been overriden and holds general information that the overriden subclass method still needs access to
The == operator in Java
1. performs a reference equality check for object operands
2. and a content/value equality check for primitive type operands.
The equals method in Java is used to check for content equality of two separate objects, and its implementation must be overridden for the desired functionality.
What is the difference between the == operator and the equals method in Java?
Describe the method lookup process without inheritance or polymorphism.
The process for this is as follows:
1. accessing the variable
2. finding the object stored in that variable
3. finding the class of the object
4. executing the implementation of the method in the class.
How does a hashset determine whether an element should be added to a bucket?
When adding a new element to this:
1. the hash table bucket is determined by the object’s hashCode() method
2. If the bucket already contains an equal element, the new element is not added because it would be a duplicate
Note:
Determining equality in step 2 is achieved using the elements equals() method
when
overiding the equals() method,
what are 3 things to keep in mind
when overiding this:
1. its siganture is - equals(Object)
2. it returns a boolean
3. we want to compare the state of this object with the object given in the formal parameter
What 3 things must be kept in mind when overriding the toString method?
When overriding this method, it must
1. match the method’s signature
2. be used to give a string representation of the object
3. have no parameters while returning a string.
define
Dynamic type
This type is the type of the object that the variable holds at any point during runtime. It is determined at runtime and can be different from the static type, where subtyping and substitution are in use
This is a method inherited from the class Object
This is used to return an integer value generated from the fields of an object, and is used by data structures such as hashmap and hashset.
It should be used in conjunction with the equals() method, and if we plan on using our objects within these data structures, it becomes important that we have overridden both of these methods.
What is the
hashCode() method
used for in Java?
When overriding this method, it must
1. match the method’s signature
2. be used to give a string representation of the object
3. have no parameters while returning a string.
What 3 things must be kept in mind when overriding the toString method?
when overiding equals()
When should instanceof be used over getClass()?
If we want the superclass to take into account that subclasses may be passed in and maintain the Liskov substitution principle, then we would prefer using instanceof.
define
Compile-time error
this error is an error that is detected by the compiler during the compilation process.
If, for example, a method is called on an object that does not have that method, a compile-time error will occur.
Why do we need hashcodes in Java?
These are used by data structures such as hashsets and hashmaps, which use an underlying data structure called a hash table.
These are used to determine which bucket of the hash table an element should go into, and whether that bucket already contains an equal element.
Hashsets and hashmaps rely on both the hashcode() and equals() methods to work correctly.
This method in Java is used to compare the state (content equality) of two separate objects. It is inherited from the Object class and its default implementation only checks for reference equality.
What is the purpose of the
equals method
in Java?
define
Static type
This is the type of the variable declared in the source code. It is the type that the compiler uses for type checking.
describe the
protected access modifier
Accessible to code in the class in which it is defined, subclasses (direct or indirect) and classes in the same package
To override this method in Java, we use the signature “hashCode()”.
It should return an int generated from the same fields that the equals() method uses, so that content equality is tied to the fields chosen.
How do we
**override the hashCode() **
method in Java?
these include:
1. public
2. private
3. protected
4. package
what are the 4
access modifiers
in java
describe the
public access modifier
Accessible to code in any class
(whether or not the classes are in the same package or related by inheritance)
What is
getClass() method
used for?
This is used to return the runtime type of an object.
This is a Java access modifier that offers a middle ground between the private and public access modifiers.
It is accessible to code in the class in which it is defined, subclasses (direct or indirect), and classes in the same package.
What is the
**protected access modifier **
in Java?
What is the
hashCode() method
used for in Java?
This is a method inherited from the class Object
This is used to return an integer value generated from the fields of an object, and is used by data structures such as hashmap and hashset.
It should be used in conjunction with the equals() method, and if we plan on using our objects within these data structures, it becomes important that we have overridden both of these methods.
How do we
**override the hashCode() **
method in Java?
To override this method in Java, we use the signature “hashCode()”.
It should return an int generated from the same fields that the equals() method uses, so that content equality is tied to the fields chosen.
This is possible because of polymorphic variables, which are variables that can hold different types at runtime.
in turn meaning we can call different method implementations
what allows for
method polymorphism
This is where the method that will be executed is decided at runtime when inheritance, polymorphism and overriding are in use.
What is
dynamic method lookup?
What is the purpose of the
equals method
in Java?
This method in Java is used to compare the state (content equality) of two separate objects. It is inherited from the Object class and its default implementation only checks for reference equality.
what allows for
method polymorphism
This is possible because of polymorphic variables, which are variables that can hold different types at runtime.
in turn meaning we can call different method implementations
when performing this keep in mind that:
1. Remember that the fields we use must match the fields used in the overridden equals() method.
2. It is up to us to decide what fields are required to mean equality.
For example, in a Student class with fields id, name, and classes, if every student has a unique id, we can simply use the id to check for content equality.
when creating a hashcode within an overriden hashCode() method
what are 2 things to keep in mind
define
Type checking
is the process of verifying that the types of values and expressions in a program are correct and consistent.
What are the two questions that are being asked when checking if two things are equal in Java?
When checking if two things are equal in Java, there are two questions being asked:
- Are the two object references equal (reference equality)?
- Are the states of two separate objects equal (content equality)?
these allow a variable to hold objects of different types. For example, a variable of type Vehicle can hold an object of type Car or Van.
what does
Subtyping and substitution
allow
write the code that overrides the hashCode method and returns a hash code
implementaion:
import java.util.Objects; public int hashCode() { return Objects.hash(author, timestamp); }
This is used to return the runtime type of an object.
What is
getClass() method
used for?
What is the purpose of overriding the toString method?
The purpose of overriding this method is to give a more useful string representation of an object, which is especially useful for debugging purposes.