Chapter 10 - Improving structure with inheritance Flashcards

1
Q

What is the
principle of inheritance
in Java?

A

this in Java is an abstraction technique that allows us to categorize things based on shared common elements and unique elements.

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

from the following classes create an example
Inheritance hierarchy

Classes: animal, mammal, dog, cat, poodle, dalmation

A

example:
animal is the superclass, mammal is a subclass, and dog and cat are subclasses of mammal, and poodle and dalmatian are subclasses of dog.

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

describe the 6 steps that take place when an object is initialised

A

1.Sufficient memory for the new object is allocated.
2.The object’s fields are given default ‘zero’ values: Reference type fields are assignednull, numerical type primitive fields are assigned0, floating point primitives are assigned0.0andbooleanfields are assignedfalse. Achartype field is also assigned a value of0that is equivalent to the null Unicode character.
3.The constructor is called. This might involve also calling another constructor in the same class as the first line of code in the constructor, usingthis().
4.A parent class constructor is called (except for classObject), either explicitly usingsuper()or implicitly.
5.Any initialisation of fields that takes place outside of the constructor (e.g. static constant initialisation) is performed.
6.The rest of the constructor code is executed.

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

What is a
subtype
in Java?

A

In Java, this is a type that has a supertype. This may be substituted in places where its supertype has been declared or is expected

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

this in Java is a variable that can reference different object types at runtime, including its declared type and any subtypes of the declared type.

A

What is a
polymorphic variable
in Java?

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

How is
code duplication
a waste of time?

A

this is a waste of time because creating duplicate code for a new feature that has common code to an existing feature is inefficient.

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

With inheritance in Java, what are subtypes?

A

these in Java are the subclasses of a superclass.

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

What is a
class diagram
used for and what information will it illustrate?

A

this diagram is used to illustrate the structure of a system by showing the classes in the system

information included would be:
1. Class name
2. Class fields
3. Class methods

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

this would be a wise decision if we have two constructors that have identical code.

in this case we can have one constructor that sets the duplicate code while the other can set its unique values and call the other constructor for any duplicate code

A

when would it be wise to
reuse constructor code

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

What is
code duplication

A

this is where similar data or operations are stored in multiple parts of the source code.

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

this is a constructor placed within the bytecode by java if the class is inheriting and the programmer has not explicitly called its superclass constructor

note:
the call is a zero argument call to the superclass constructor i.e. super()

A

describe the
The default constructor

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

give 3 notes about calling
this() within a constructor

A

notes include:
1. must be the first line of code in a constructor
2. It will find and call a constructor with the matching signature in the same class
3. may not be used in the same constructor calling super()

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

these include:
1. Avoids code duplication - identical source code is avoided
2. Code reuse - existing code can be reused. E.g. if a class already exists that contains similar functionality we may be able to create a subclass of this instead of rewriting the code again
3. Easier maintenance - maintenance is easier changes can be made in one place that may be shared by many classes
4. Extendibility - adding new funcionality can be much easier when inheritance has been implemented. E.g. we could add a new feature as a subclass of an already existing and similar superclass

A

what are 4 advantages that come from using
inheritance

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

describe the
this() keyword

A

when this is used within a constructor it will search for a constructor in the same class with a matching signature

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

this in Java is a structure where many classes are linked through an inheritance relationship, and subclasses are themselves superclasses for other subclasses.

A

What is an
inheritance hierarchy
in Java?

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

What is a
**ClassCastException **
in Java?

A

this in Java occurs when the java runtime system checks whether or not a variable really holds an object of a certain type, and it turns out that the variable actually holds a different subtype other than the one specified in the cast operator.

This can occur if the type specified in the cast operator is incorrect or if the variable actually holds a different subtype.

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

describe the
general rule for variables VS the correct rule

A
  1. The general rule for variables in Java is that their type must match the type of the object they are initialized with.
  2. The correct rule for variables in java is that a variable declared with a supertype may be initialised with any subtype of the declared supertype variable
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

What does the
‘extends’ keyword
in Java do?

A

this keyword in Java is used to specify that a class is inheriting from a superclass.

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

3 methods include:
1. toString() - which returns a string representation of the object
2. equals() - which compares the object to another object for equality
3. hashCode() - which returns a hash code value for the object.

A

name 3 methods provided by the Object class in Java?

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

Can Java automatically call a superclass constructor for us if we don’t use the ‘super’ keyword?

A

Yes, Java will automatically call a superclass constructor for us if we don’t use the ‘super’ keyword.

However, this will result in a compile-time error if there is no zero-argument superclass constructor.

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

What is
inheritance
in Java?

A

this is a feature in Java that allows us to define a class that extends another class, known as the superclass.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q
  1. The general rule for variables in Java is that their type must match the type of the object they are initialized with.
  2. The correct rule for variables in java is that a variable declared with a supertype may be initialised with any subtype of the declared supertype variable
A

describe the
general rule for variables VS the correct rule

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

When might casting be necessary in Java?

A

this is sometimes necessary when the compiler experiences what is known as “type loss”

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

What is a
superclass
in Java?

A

this is a class that is extended by another class and can contain common code that is inherited by its subclasses.

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

How is
**code duplication **
inflexible?

A

this is inflexible because changes must be made to every piece of duplicate code if a change is made.

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

In Java, this refers to the ability to pass a subtype object where a supertype is expected, provided that the subtype given is a subtype of the declared supertype.

A

What does the concept of
**substitution **
refer to in Java?

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

this is where similar data or operations are stored in multiple parts of the source code.

A

What is
code duplication

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

What is the general rule of
substitution
in Java?

A

The general rule of this in Java is that you cannot assign a supertype to a subtype.

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

It may be necessary to perform this if a new subclass is added but it differs from its sibling subclasses in that it does not require or should not have some of the fields that it will inherit from its superclass.

A

When might it be necessary to
**refactor an inheritance hierarchy **
in Java?

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

Can public methods of a superclass be accessed by a subclass in Java?

A

Yes, any public methods of a superclass can be accessed from a subclass using the simple name, as if it were in the subclass itself.

We don’t need to use dot notation and specify the fully qualified name.

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

The disadvantages of this include:

  1. inflexibility
  2. error-proneness
  3. waste of time
A

What are the disadvantages of
code duplication?

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

these in Java are the subclasses of a superclass.

A

With inheritance in Java, what are subtypes?

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

notes include:
1. must be the first line of code in a constructor
2. It will find and call a constructor with the matching signature in the same class
3. may not be used in the same constructor calling super()

A

give 3 notes about calling
this() within a constructor

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

In Java, when can substitution be used for formal and actual parameters?

A

In Java, substitution can be used for:

1. formal parameter - can be declared with a supertype
2. actual parameter - can accept the supertype as an argument or any subtype of the expected supertype.

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

this is a class that is extended by another class and can contain common code that is inherited by its subclasses.

A

What is a
superclass
in Java?

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

What are the members of a class in Java?

A

Members of a class in Java include fields and methods.

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

What is a
subclass
in Java?

A

this is a class that extends another class (its superclass) and inherits all of its fields and methods.

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

this is a class that extends another class (its superclass) and inherits all of its fields and methods.

A

What is a
subclass
in Java?

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

describe the
The default constructor

A

this is a constructor placed within the bytecode by java if the class is inheriting and the programmer has not explicitly called its superclass constructor

note:
the call is a zero argument call to the superclass constructor i.e. super()

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

What is an
inheritance hierarchy
in Java?

A

this in Java is a structure where many classes are linked through an inheritance relationship, and subclasses are themselves superclasses for other subclasses.

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

this is a feature in Java that allows us to define a class that extends another class, known as the superclass.

A

What is
inheritance
in Java?

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

How is
code duplication
error-prone?

A

this is error-prone because someone else making changes later may not realize that there is duplicate code that also must carry the same changes.

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

during initialisation how do we pass values from a subclass to its superclass in Java?

A

To achieve this we use the keyword ‘super’ to call the superclass constructor.

44
Q

reasons include:
1. Redundant code in the class
2. There is a risk that in the future another developer may try and process the fields which could result in bad behavior
3. It is untidy and leaves ambiguity in the class

A

What are the reasons that make it bad practice to let the fields be inherited and not used in a class?

45
Q

this should be used as the first statement within a Java subclass constructor.

A

In what order should the
**super keyword **
be used in a Java subclass constructor?

46
Q

The general rule of this in Java is that you cannot assign a supertype to a subtype.

A

What is the general rule of
substitution
in Java?

47
Q

What does the
super
keyword do in Java?

A

In Java, this keyword is used to call a superclass constructor from a subclass constructor, to properly initialize the superclass.

48
Q

In Java, this is a type which has subtypes.

The subtypes may be substituted in place of the supertype where a supertype has been declared or is expected.

A

What is a
supertype
in Java?

49
Q

this in Java is the conversion of one type to another type using the cast operator.

A

What is
**casting **
in Java?

50
Q

using the following code create a scenario where the compiler would experience type loss

Vehicle v;
Car c = new Car();
A

example:

Vehicle v;
Car c = new Car();
v = c;  // correct
c = v;  // compile error

In the last line we get an error even though we know that v holds an object of the same type as the c variable so this should technically work

51
Q

How is the
**extends keyword **
used in Java?

A

This keyword is used in the header of a subclass to specify that it is extending a superclass

example:
“public class MessagePost extends Post”.

52
Q

this in Java is an abstraction technique that allows us to categorize things based on shared common elements and unique elements.

A

What is the
principle of inheritance
in Java?

53
Q

describe
**type loss **

A

this is a phenomenon experienced by the java compiler

it occurs when a legal assignment has been made but the compiler does not accept it because it sees the declared variable type being incompatible with the type of variable we are attempting to initialise it with.

even though the assignment could be legal

54
Q

To achieve this we use the keyword ‘super’ to call the superclass constructor.

A

during initialisation how do we pass values from a subclass to its superclass in Java?

55
Q

What are the reasons that make it bad practice to let the fields be inherited and not used in a class?

A

reasons include:
1. Redundant code in the class
2. There is a risk that in the future another developer may try and process the fields which could result in bad behavior
3. It is untidy and leaves ambiguity in the class

56
Q

the java compiler experiences this for two reasons:

  1. java looks at and compiles each line in isolation
  2. objects are not created untill runtime

Vehicle v;
Car c = new Car();
v = c; // correct
c = v; // compile error

from the code above the compiler sees that we are trying to initialise a subtype variable with a supertype variable. THIS IS AN ILLEGAL ASSIGNMENT
as far as the compiler is concerned

A

explain why the java compiler experiences type loss

57
Q

What is a
polymorphic variable
in Java?

A

this in Java is a variable that can reference different object types at runtime, including its declared type and any subtypes of the declared type.

58
Q

name 3 methods provided by the Object class in Java?

A

3 methods include:
1. toString() - which returns a string representation of the object
2. equals() - which compares the object to another object for equality
3. hashCode() - which returns a hash code value for the object.

59
Q

The benefit of using these in Java is that it allows for flexibility and code reusability, as variables can be declared with a supertype and then be initialized with that supertype or any of its subtypes.

A

What is the
benefit of using polymorphic variables
in Java?

60
Q

this is inflexible because changes must be made to every piece of duplicate code if a change is made.

A

How is
**code duplication **
inflexible?

61
Q

What are the disadvantages of
code duplication?

A

The disadvantages of this include:

  1. inflexibility
  2. error-proneness
  3. waste of time
62
Q

In what order should the
**super keyword **
be used in a Java subclass constructor?

A

this should be used as the first statement within a Java subclass constructor.

63
Q

example:

Vehicle v;
Car c = new Car();
v = c;  // correct
c = v;  // compile error

In the last line we get an error even though we know that v holds an object of the same type as the c variable so this should technically work

A

using the following code create a scenario where the compiler would experience type loss

Vehicle v;
Car c = new Car();
64
Q

this is a waste of time because creating duplicate code for a new feature that has common code to an existing feature is inefficient.

A

How is
code duplication
a waste of time?

65
Q

What is
UML

A

this stands for Unified Modeling Language, and it is a visual modeling language used for software engineering.

66
Q

these can’t be called by name in Java because their main purpose is to initialize objects and not to be used like methods.

A

Why can’t
constructors
be called by name in Java?

67
Q

this stands for Unified Modeling Language, and it is a visual modeling language used for software engineering.

A

What is
UML

68
Q

What is the
Object class
in Java?

A

this is a special class in the Java standard library that serves as the superclass of all classes in Java, either directly or indirectly.

it is also the only class in a java program that does not itself have superclass

69
Q

In Java, this is a type that has a supertype. This may be substituted in places where its supertype has been declared or is expected

A

What is a
subtype
in Java?

70
Q

In Java, this keyword is used to call a superclass constructor from a subclass constructor, to properly initialize the superclass.

A

What does the
super
keyword do in Java?

71
Q

this is error-prone because someone else making changes later may not realize that there is duplicate code that also must carry the same changes.

A

How is
code duplication
error-prone?

72
Q

The two purposes include:
1. to allow the declaration of polymorphic variables of type Object, which can accept any object type (not often used but in special cases can be beneficial)
2. to provide common methods that will be available to every created object, such as toString, equals, and hashCode.

A

What are the two purposes of
having a common superclass of all classes
in Java?

73
Q

1.Sufficient memory for the new object is allocated.
2.The object’s fields are given default ‘zero’ values: Reference type fields are assignednull, numerical type primitive fields are assigned0, floating point primitives are assigned0.0andbooleanfields are assignedfalse. Achartype field is also assigned a value of0that is equivalent to the null Unicode character.
3.The constructor is called. This might involve also calling another constructor in the same class as the first line of code in the constructor, usingthis().
4.A parent class constructor is called (except for classObject), either explicitly usingsuper()or implicitly.
5.Any initialisation of fields that takes place outside of the constructor (e.g. static constant initialisation) is performed.
6.The rest of the constructor code is executed.

A

describe the 6 steps that take place when an object is initialised

74
Q

when this is used within a constructor it will search for a constructor in the same class with a matching signature

A

describe the
this() keyword

75
Q

this keyword in Java is used to specify that a class is inheriting from a superclass.

A

What does the
‘extends’ keyword
in Java do?

76
A

ignore

77
Q

Do access modifiers of superclass members apply to subclasses in Java?

A

Yes, access modifiers of superclass members still apply to subclasses in Java. For example, private fields of a superclass cannot be accessed by a subclass.

78
Q

When might it be necessary to
**refactor an inheritance hierarchy **
in Java?

A

It may be necessary to perform this if a new subclass is added but it differs from its sibling subclasses in that it does not require or should not have some of the fields that it will inherit from its superclass.

79
Q

What is the
benefit of using inheritance in Java?

A

using this allows us to create a class that holds common code (the superclass) and then create subclasses that inherit all of its fields and methods while also implementing their own specialized fields and methods.

by doing this we can avoid the downfalls of code duplication

80
Q

What is
**casting **
in Java?

A

this in Java is the conversion of one type to another type using the cast operator.

81
Q

What is the
benefit of using polymorphic variables
in Java?

A

The benefit of using these in Java is that it allows for flexibility and code reusability, as variables can be declared with a supertype and then be initialized with that supertype or any of its subtypes.

82
Q

Can you provide an example of how polymorphic variables can be used in Java?

A

An example of how polymorphic variables can be used in Java is when two objects are given a superclass, and a loop can be used to loop over the superclass type.

By using polymorphic variables, both subtypes can be held as variables in the loop if the supertype is declared. This allows for more efficient and reusable code.

83
Q

give 2 reasons as to why it is good style to explicitly call the superclass constructor in a Java subclass?

A

It is good style to explicitly call the superclass constructor in a Java subclass because:

  1. it makes it clear that there is always a call to the superclass
  2. it ensures that the superclass is properly initialized.
84
Q

this is a phenomenon experienced by the java compiler

it occurs when a legal assignment has been made but the compiler does not accept it because it sees the declared variable type being incompatible with the type of variable we are attempting to initialise it with.

even though the assignment could be legal

A

describe
**type loss **

85
Q

Are constructors considered as members of a class in Java?

A

No, constructors are not considered as members of a class in Java.

86
Q

this in Java occurs when the java runtime system checks whether or not a variable really holds an object of a certain type, and it turns out that the variable actually holds a different subtype other than the one specified in the cast operator.

This can occur if the type specified in the cast operator is incorrect or if the variable actually holds a different subtype.

A

What is a
**ClassCastException **
in Java?

87
Q

Do private fields and methods get inherited by subclasses in Java?

A

No, private fields and methods are not inherited by subclasses in Java and hence will not be members of any subclasses.

88
Q

Why can’t
constructors
be called by name in Java?

A

these can’t be called by name in Java because their main purpose is to initialize objects and not to be used like methods.

89
Q

How can a subclass access private fields of its superclass in Java?

A

A subclass can’t directly access private fields of its superclass in Java.

If access is required, the superclass must create public methods to provide access to these fields.

90
Q

What is the advantage of separating the responsibilities of constructors from methods in Java?

A

Separating the responsibilities of constructors from methods in Java allows for predictable initialization of objects.

91
Q

In Java, substitution can be used for:

1. formal parameter - can be declared with a supertype
2. actual parameter - can accept the supertype as an argument or any subtype of the expected supertype.

A

In Java, when can substitution be used for formal and actual parameters?

92
Q

using this allows us to create a class that holds common code (the superclass) and then create subclasses that inherit all of its fields and methods while also implementing their own specialized fields and methods.

by doing this we can avoid the downfalls of code duplication

A

What is the
benefit of using inheritance in Java?

93
Q

this is a special class in the Java standard library that serves as the superclass of all classes in Java, either directly or indirectly.

it is also the only class in a java program that does not itself have superclass

A

What is the
Object class
in Java?

94
Q

this is achieved by making a call from one constructor to another constructor using the this() keyword

A

how do we actually
reuse constructor code

95
Q

What is a
supertype
in Java?

A

In Java, this is a type which has subtypes.

The subtypes may be substituted in place of the supertype where a supertype has been declared or is expected.

96
Q

explain why the java compiler experiences type loss

A

the java compiler experiences this for two reasons:

  1. java looks at and compiles each line in isolation
  2. objects are not created untill runtime

Vehicle v;
Car c = new Car();
v = c; // correct
c = v; // compile error

from the code above the compiler sees that we are trying to initialise a subtype variable with a supertype variable. THIS IS AN ILLEGAL ASSIGNMENT
as far as the compiler is concerned

97
Q

What does the concept of
**substitution **
refer to in Java?

A

In Java, this refers to the ability to pass a subtype object where a supertype is expected, provided that the subtype given is a subtype of the declared supertype.

98
Q

when would it be wise to
reuse constructor code

A

this would be a wise decision if we have two constructors that have identical code.

in this case we can have one constructor that sets the duplicate code while the other can set its unique values and call the other constructor for any duplicate code

99
Q

example:
animal is the superclass, mammal is a subclass, and dog and cat are subclasses of mammal, and poodle and dalmatian are subclasses of dog.

A

from the following classes create an example
Inheritance hierarchy

Classes: animal, mammal, dog, cat, poodle, dalmation

100
Q

This keyword is used in the header of a subclass to specify that it is extending a superclass

example:
“public class MessagePost extends Post”.

A

How is the
**extends keyword **
used in Java?

101
Q

What are the two purposes of
having a common superclass of all classes
in Java?

A

The two purposes include:
1. to allow the declaration of polymorphic variables of type Object, which can accept any object type (not often used but in special cases can be beneficial)
2. to provide common methods that will be available to every created object, such as toString, equals, and hashCode.

102
Q

what are 4 advantages that come from using
inheritance

A

these include:
1. Avoids code duplication - identical source code is avoided
2. Code reuse - existing code can be reused. E.g. if a class already exists that contains similar functionality we may be able to create a subclass of this instead of rewriting the code again
3. Easier maintenance - maintenance is easier changes can be made in one place that may be shared by many classes
4. Extendibility - adding new funcionality can be much easier when inheritance has been implemented. E.g. we could add a new feature as a subclass of an already existing and similar superclass

103
Q

how do we actually
reuse constructor code

A

this is achieved by making a call from one constructor to another constructor using the this() keyword

104
Q

this is sometimes necessary when the compiler experiences what is known as “type loss”

A

When might casting be necessary in Java?

105
Q

this diagram is used to illustrate the structure of a system by showing the classes in the system

information included would be:
1. Class name
2. Class fields
3. Class methods

A

What is a
class diagram
used for and what information will it illustrate?