Chapter 11 - More about inheritance Flashcards

1
Q

This takes place when a variable is polymorphic and we plan to call a method on the object that it will hold.

A

When does
dynamic method lookup take place?

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

when overiding equals()
When should getClass() be used over instanceof?

A

If we want to avoid the behavior of a superclass taking into account that subclasses may be passed in, then getClass() is preferred.

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

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.

A

Why do we need hashcodes in Java?

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

What is
method polymorphism?

A

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

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

what does
Subtyping and substitution
allow

A

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.

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

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.

A

Why should we use immutable objects as keys in hash-based structures?

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

What 2 things are important to keep in mind when using
getClass()?

A

when using this 2 things to keep in mind are:

  1. It will invoke a null pointer exception if a variable has not been initialized and is null.
  2. It takes into account only the current runtime object and nothing else.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

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.

A

What are 2 reasons for the
efficiency of hash tables?

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

What is a super call in methods?

A

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

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

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.

A

What is the difference between the == operator and the equals method in Java?

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

Describe the method lookup process without inheritance or polymorphism.

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How does a hashset determine whether an element should be added to a bucket?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

when
overiding the equals() method,
what are 3 things to keep in mind

A

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

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

What 3 things must be kept in mind when overriding the toString method?

A

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.

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

define
Dynamic type

A

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

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

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.

A

What is the
hashCode() method
used for in Java?

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

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.

A

What 3 things must be kept in mind when overriding the toString method?

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

when overiding equals()
When should instanceof be used over getClass()?

A

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.

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

define
Compile-time error

A

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.

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

Why do we need hashcodes in Java?

A

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.

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

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.

A

What is the purpose of the
equals method
in Java?

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

define
Static type

A

This is the type of the variable declared in the source code. It is the type that the compiler uses for type checking.

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

describe the
protected access modifier

A

Accessible to code in the class in which it is defined, subclasses (direct or indirect) and classes in the same package

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

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.

A

How do we
**override the hashCode() **
method in Java?

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

these include:
1. public
2. private
3. protected
4. package

A

what are the 4
access modifiers
in java

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

describe the
public access modifier

A

Accessible to code in any class

(whether or not the classes are in the same package or related by inheritance)

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

What is
getClass() method
used for?

A

This is used to return the runtime type of an object.

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

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.

A

What is the
**protected access modifier **
in Java?

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

What is the
hashCode() method
used for in Java?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
30
Q

How do we
**override the hashCode() **
method in Java?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
31
Q

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

A

what allows for
method polymorphism

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

This is where the method that will be executed is decided at runtime when inheritance, polymorphism and overriding are in use.

A

What is
dynamic method lookup?

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

What is the purpose of the
equals method
in Java?

A

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.

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

what allows for
method polymorphism

A

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

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

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.

A

when creating a hashcode within an overriden hashCode() method
what are 2 things to keep in mind

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

define
Type checking

A

is the process of verifying that the types of values and expressions in a program are correct and consistent.

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

What are the two questions that are being asked when checking if two things are equal in Java?

A

When checking if two things are equal in Java, there are two questions being asked:

  1. Are the two object references equal (reference equality)?
  2. Are the states of two separate objects equal (content equality)?
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
38
Q

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.

A

what does
Subtyping and substitution
allow

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

write the code that overrides the hashCode method and returns a hash code

A

implementaion:

import java.util.Objects;

public int hashCode() {
    return Objects.hash(author, timestamp);
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
40
Q

This is used to return the runtime type of an object.

A

What is
getClass() method
used for?

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

What is the purpose of overriding the toString method?

A

The purpose of overriding this method is to give a more useful string representation of an object, which is especially useful for debugging purposes.

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

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.

A

define
Compiler
and one of its jobs

43
Q

define
Object creation

A

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.

44
Q

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.

A

describe the following annotation
@Override

45
Q

describe the
private access modifier

A

Accessible to code only in the class in which it is defined

46
A

ignore

47
Q

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

A

What is
method polymorphism?

48
Q

What is the
toString method
in Java?

A

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.

49
Q

describe the
package access modifier

A

Accessible to code in the class in which it is defined and classes in the same package

50
Q

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.

A

define
Compile-time error

51
Q

Accessible to code in any class

(whether or not the classes are in the same package or related by inheritance)

A

describe the
public access modifier

52
Q

This is the type of the variable declared in the source code. It is the type that the compiler uses for type checking.

A

define
Static type

53
Q

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

A

How does a hashset determine whether an element should be added to a bucket?

54
Q

What are three ways to implement the equals method in Java to compare the states of two objects?

A

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.

55
Q

Describe the method lookup process with inheritance, polymorphic variable and method overriding.

A

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

56
Q

How can the toString method make code more flexible?

A

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.

57
Q

What are 3 differences between super calls in methods and constructors?

A
  1. Super calls in methods require an explicit method name, while super calls in constructors do not.
  2. super calls in methods can appear at any time during execution, while super calls in constructors must be the first statement.
  3. Finally, no automatic super call is created or required in methods, unlike in constructors.
58
Q

is the process of verifying that the types of values and expressions in a program are correct and consistent.

A

define
Type checking

59
Q

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.

A

when overiding equals()
When should instanceof be used over getClass()?

60
Q

what are the 4
access modifiers
in java

A

these include:
1. public
2. private
3. protected
4. package

61
Q

What is the
**protected access modifier **
in Java?

A

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.

62
Q

describe the following annotation
@Override

A

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.

63
Q

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.

A

What are three ways to implement the equals method in Java to compare the states of two objects?

64
Q

What is the
instanceof operator?

A

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.

65
Q

What happens in method lookup with inheritance and no polymorphism?

A

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.

66
Q

when using this 2 things to keep in mind are:

  1. It will invoke a null pointer exception if a variable has not been initialized and is null.
  2. It takes into account only the current runtime object and nothing else.
A

What 2 things are important to keep in mind when using
getClass()?

67
Q

define
Overriding

A

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.

68
Q

what are 2 guidlines that will improve the efficiency of a hash table

A

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.

69
Q

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.

A

What are 2 key points to keep in mind about the
protected access modifier in Java?

70
Q

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.

A

What is 3 purposes of using instanceof?

71
Q

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

A

when
overiding the equals() method,
what are 3 things to keep in mind

72
Q

Accessible to code in the class in which it is defined, subclasses (direct or indirect) and classes in the same package

A

describe the
protected access modifier

73
Q

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

A

define
Dynamic type

74
Q

What is
dynamic method lookup?

A

This is where the method that will be executed is decided at runtime when inheritance, polymorphism and overriding are in use.

75
Q

what 2 reasons are there as to why the compiler only uses static types to perform its type checking

A

reasons include:
1. Objects are not created until runtime (object creation)
2. We do not necessarily know beforehand which subtypes will be substituted

76
Q

What are 2 reasons for the
efficiency of hash tables?

A

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.

77
Q

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.

A

When is a super call necessary in a method?

78
Q

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.

A

What is the
toString method
in Java?

79
Q

do subclasses inherit the private members of its superclass

A

No, Subclasses do not inherit the private fields and methods of their superclass.

80
Q

If we want to avoid the behavior of a superclass taking into account that subclasses may be passed in, then getClass() is preferred.

A

when overiding equals()
When should getClass() be used over instanceof?

81
Q

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.

A

define
Overriding

82
Q

Accessible to code in the class in which it is defined and classes in the same package

A

describe the
package access modifier

83
Q

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

A

Describe the method lookup process with inheritance, polymorphic variable and method overriding.

84
Q

When is a super call necessary in a method?

A

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.

85
Q

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.

A

What is the
instanceof operator?

86
Q

What is the difference between the == operator and the equals method in Java?

A

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.

87
Q

What are 2 key points to keep in mind about the
protected access modifier in Java?

A

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.

88
Q

implementaion:

import java.util.Objects;

public int hashCode() {
    return Objects.hash(author, timestamp);
}
A

write the code that overrides the hashCode method and returns a hash code

89
Q

define
Compiler
and one of its jobs

A

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.

90
Q

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.

A

describe the
default access level

91
Q

with inheritance does the superclass know about the methods of its subclasses

A

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.

92
Q

will instanceof operator throw NullPointerException
if passed a null object

A

No, it will instead return false. meaning we do not have to worry about checking for null objects

93
Q

when creating a hashcode within an overriden hashCode() method
what are 2 things to keep in mind

A

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.

94
Q

The purpose of overriding this method is to give a more useful string representation of an object, which is especially useful for debugging purposes.

A

What is the purpose of overriding the toString method?

95
Q

describe the
default access level

A

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.

96
Q

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

A

What is a super call in methods?

97
Q

What is 3 purposes of using instanceof?

A

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.

98
Q

Why should we use immutable objects as keys in hash-based structures?

A

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.

99
Q

reasons include:
1. Objects are not created until runtime (object creation)
2. We do not necessarily know beforehand which subtypes will be substituted

A

what 2 reasons are there as to why the compiler only uses static types to perform its type checking

100
Q

When does
dynamic method lookup take place?

A

This takes place when a variable is polymorphic and we plan to call a method on the object that it will hold.

101
Q

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.

A

define
Object creation

102
Q

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.

A

what are 2 guidlines that will improve the efficiency of a hash table

103
Q

Accessible to code only in the class in which it is defined

A

describe the
private access modifier