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.
This is a program that translates source code into machine code. It performs various checks, including type checking, to ensure that the program is correct and can be executed.
define
Compiler
and one of its jobs
define
Object creation
This is the process of creating an instance of a class. It involves allocating memory for the object and initializing its fields. and is performed at runtime.
This is an annotation that can be added before a method in a subclass that overrides a method in its superclass.
It indicates that the method is intended to override an existing method in the superclass.
describe the following annotation
@Override
describe the
private access modifier
Accessible to code only in the class in which it is defined
ignore
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 is
method polymorphism?
What is the
toString method
in Java?
This method is inherited by every object in Java from the Object class.
If the method is called without being overridden, it will print the classname@memoryaddress of the dynamic type that the call originated from.
describe the
package access modifier
Accessible to code in the class in which it is defined and classes in the same package
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.
define
Compile-time error
Accessible to code in any class
(whether or not the classes are in the same package or related by inheritance)
describe the
public access modifier
This is the type of the variable declared in the source code. It is the type that the compiler uses for type checking.
define
Static type
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
How does a hashset determine whether an element should be added to a bucket?
What are three ways to implement the equals method in Java to compare the states of two objects?
implementations of this include:
1a. Perform a reference check
2a. perform an instanceof check
3a. cast the object to the correct type
3a. compare the fields.
1b. perform an instanceof check
2b. cast the object to the correct type
3b. compare the fields.
1c. Check if the passed object is not null and check class using getClass()
2c. cast the object to the correct type
3c. compare the fields.
Describe the method lookup process with inheritance, polymorphic variable and method overriding.
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.
assuming that the dynamic variable has overriden the method
How can the toString method make code more flexible?
The toString method can make code more flexible as it returns a string that can be used in different ways, such as
1. displaying it to the CLI/GUI
2. saving it to a file
3. sending it over a network
4. displaying it in a browser.
Additionally, System.out.print and println will use the object’s toString method if it is passed an argument that is not a string.
What are 3 differences between super calls in methods and constructors?
- Super calls in methods require an explicit method name, while super calls in constructors do not.
- super calls in methods can appear at any time during execution, while super calls in constructors must be the first statement.
- Finally, no automatic super call is created or required in methods, unlike in constructors.
is the process of verifying that the types of values and expressions in a program are correct and consistent.
define
Type checking
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.
when overiding equals()
When should instanceof be used over getClass()?
what are the 4
access modifiers
in java
these include:
1. public
2. private
3. protected
4. package
What is the
**protected access modifier **
in Java?
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.
describe the following annotation
@Override
This is an annotation that can be added before a method in a subclass that overrides a method in its superclass.
It indicates that the method is intended to override an existing method in the superclass.
implementations of this include:
1a. Perform a reference check
2a. perform an instanceof check
3a. cast the object to the correct type
3a. compare the fields.
1b. perform an instanceof check
2b. cast the object to the correct type
3b. compare the fields.
1c. Check if the passed object is not null and check class using getClass()
2c. cast the object to the correct type
3c. compare the fields.
What are three ways to implement the equals method in Java to compare the states of two objects?
What is the
instanceof operator?
This operator is used to check if an object (the left operand) is an instance of a class (the right operand), or an instance of one of its direct or indirect subclasses.
What happens in method lookup with inheritance and no 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. if a method is not found, the superclass is searched and the method will be executed. This behavior would continue all the way up the hierarchy until the method is found.
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.
What 2 things are important to keep in mind when using
getClass()?
define
Overriding
also known as redefinition, is the process of defining a method in a subclass with the same signature as a method in its superclass.
The method in the subclass overrides the method in the superclass.
what are 2 guidlines that will improve the efficiency of a hash table
guidelines for this include:
1. Equal objects should have the same hash code, so they can land in the same bucket, reducing search time for identical objects.
2. Unequal objects can also have the same hash code, ensuring that the elements are spread evenly across the buckets. This helps avoid having one bucket with only one element, which can break the efficiency of a hash table.
By following these guidelines, this can be optimized for efficient data storage and retrieval.
key points include:
1. This access modifier can be given to any member, but it is typically reserved for constructors or methods to avoid weakening encapsulation too much.
2. In some cases, direct access to a superclass field may require the use of this access modifier.
What are 2 key points to keep in mind about the
protected access modifier in Java?
purposes of use include:
1. check whether it is safe to cast a reference to a subclass type
2. validating that we have a particular object or a subtype
3. when iterating through collections of objects with a shared superclass or interface, allowing for specific actions to be taken on objects of a particular subtype.
What is 3 purposes of using instanceof?
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
when
overiding the equals() method,
what are 3 things to keep in mind
Accessible to code in the class in which it is defined, subclasses (direct or indirect) and classes in the same package
describe the
protected access modifier
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
define
Dynamic type
What is
dynamic method lookup?
This is where the method that will be executed is decided at runtime when inheritance, polymorphism and overriding are in use.
what 2 reasons are there as to why the compiler only uses static types to perform its type checking
reasons include:
1. Objects are not created until runtime (object creation)
2. We do not necessarily know beforehand which subtypes will be substituted
What are 2 reasons for the
efficiency of hash tables?
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.
This is necessary when method overriding has occurred and a public and overriden method from the superclass needs to be called.
Otherwise, public methods are inherited and visible, so a super call with the keyword is unnecessary.
When is a super call necessary in a method?
This method is inherited by every object in Java from the Object class.
If the method is called without being overridden, it will print the classname@memoryaddress of the dynamic type that the call originated from.
What is the
toString method
in Java?
do subclasses inherit the private members of its superclass
No, Subclasses do not inherit the private fields and methods of their superclass.
If we want to avoid the behavior of a superclass taking into account that subclasses may be passed in, then getClass() is preferred.
when overiding equals()
When should getClass() be used over instanceof?
also known as redefinition, is the process of defining a method in a subclass with the same signature as a method in its superclass.
The method in the subclass overrides the method in the superclass.
define
Overriding
Accessible to code in the class in which it is defined and classes in the same package
describe the
package access modifier
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.
assuming that the dynamic variable has overriden the method
Describe the method lookup process with inheritance, polymorphic variable and method overriding.
When is a super call necessary in a method?
This is necessary when method overriding has occurred and a public and overriden method from the superclass needs to be called.
Otherwise, public methods are inherited and visible, so a super call with the keyword is unnecessary.
This operator is used to check if an object (the left operand) is an instance of a class (the right operand), or an instance of one of its direct or indirect subclasses.
What is the
instanceof operator?
What is the difference between the == operator and the equals method in Java?
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 are 2 key points to keep in mind about the
protected access modifier in Java?
key points include:
1. This access modifier can be given to any member, but it is typically reserved for constructors or methods to avoid weakening encapsulation too much.
2. In some cases, direct access to a superclass field may require the use of this access modifier.
implementaion:
import java.util.Objects; public int hashCode() { return Objects.hash(author, timestamp); }
write the code that overrides the hashCode method and returns a hash code
define
Compiler
and one of its jobs
This is a program that translates source code into machine code. It performs various checks, including type checking, to ensure that the program is correct and can be executed.
this describes the situation where no access modifier is specified
the member defaults to package-level access, which is implicit and only available within the same package.
describe the
default access level
with inheritance does the superclass know about the methods of its subclasses
In inheritance, the relationship between a subclass and its superclass is a one-way street.
The subclass knows about the methods of its superclass, but the superclass does not know about the methods of its subclasses.
If a superclass tries to access the methods of its subclasses, it would result in an error.
will instanceof operator throw NullPointerException
if passed a null object
No, it will instead return false. meaning we do not have to worry about checking for null objects
when creating a hashcode within an overriden hashCode() method
what are 2 things to keep in mind
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.
The purpose of overriding this method is to give a more useful string representation of an object, which is especially useful for debugging purposes.
What is the purpose of overriding the toString method?
describe the
default access level
this describes the situation where no access modifier is specified
the member defaults to package-level access, which is implicit and only available within the same package.
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
What is a super call in methods?
What is 3 purposes of using instanceof?
purposes of use include:
1. check whether it is safe to cast a reference to a subclass type
2. validating that we have a particular object or a subtype
3. when iterating through collections of objects with a shared superclass or interface, allowing for specific actions to be taken on objects of a particular subtype.
Why should we use immutable objects as keys in hash-based structures?
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.
reasons include:
1. Objects are not created until runtime (object creation)
2. We do not necessarily know beforehand which subtypes will be substituted
what 2 reasons are there as to why the compiler only uses static types to perform its type checking
When does
dynamic method lookup take place?
This takes place when a variable is polymorphic and we plan to call a method on the object that it will hold.
This is the process of creating an instance of a class. It involves allocating memory for the object and initializing its fields. and is performed at runtime.
define
Object creation
guidelines for this include:
1. Equal objects should have the same hash code, so they can land in the same bucket, reducing search time for identical objects.
2. Unequal objects can also have the same hash code, ensuring that the elements are spread evenly across the buckets. This helps avoid having one bucket with only one element, which can break the efficiency of a hash table.
By following these guidelines, this can be optimized for efficient data storage and retrieval.
what are 2 guidlines that will improve the efficiency of a hash table
Accessible to code only in the class in which it is defined
describe the
private access modifier