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
How is **code duplication ** inflexible?
this is inflexible because changes must be made to every piece of duplicate code if a change is made.
26
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.
What does the concept of **substitution ** refer to in Java?
27
this is where similar data or operations are stored in multiple parts of the source code.
What is **code duplication**
28
What is the general rule of **substitution** in Java?
The general rule of this in Java is that you cannot assign a supertype to a subtype.
29
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.
When might it be necessary to **refactor an inheritance hierarchy ** in Java?
30
Can public methods of a superclass be accessed by a subclass in Java?
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.
31
The disadvantages of this include: 1. inflexibility 2. error-proneness 3. waste of time
What are the disadvantages of **code duplication?**
32
these in Java are the subclasses of a superclass.
With inheritance in Java, what are **subtypes?**
33
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()
give 3 notes about calling this() within a constructor
34
In Java, when can substitution be used for formal and actual parameters?
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.
35
this is a class that is extended by another class and can contain common code that is inherited by its subclasses.
What is a **superclass** in Java?
36
What are the members of a class in Java?
Members of a class in Java include fields and methods.
37
What is a **subclass** in Java?
this is a class that extends another class (its superclass) and inherits all of its fields and methods.
38
this is a class that extends another class (its superclass) and inherits all of its fields and methods.
What is a **subclass** in Java?
39
describe the **The default constructor**
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()
40
What is an **inheritance hierarchy** in Java?
this in Java is a structure where many classes are linked through an inheritance relationship, and subclasses are themselves superclasses for other subclasses.
41
this is a feature in Java that allows us to define a class that extends another class, known as the superclass.
What is **inheritance** in Java?
42
How is **code duplication** error-prone?
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.
43
during initialisation how do we pass values from a subclass to its superclass in Java?
To achieve this we use the keyword 'super' to call the superclass constructor.
44
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
What are the reasons that make it bad practice to let the fields be inherited and not used in a class?
45
this should be used as the first statement within a Java subclass constructor.
In what order should the **super keyword ** be used in a Java subclass constructor?
46
The general rule of this in Java is that you cannot assign a supertype to a subtype.
What is the general rule of **substitution** in Java?
47
What does the **super** keyword do in Java?
In Java, this keyword is used to call a superclass constructor from a subclass constructor, to properly initialize the superclass.
48
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.
What is a **supertype** in Java?
49
this in Java is the conversion of one type to another type using the cast operator.
What is **casting ** in Java?
50
using the following code create a scenario where the compiler would experience type loss ``` Vehicle v; Car c = new Car(); ```
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
How is the **extends keyword ** used in Java?
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
this in Java is an abstraction technique that allows us to categorize things based on shared common elements and unique elements.
What is the **principle of inheritance** in Java?
53
describe **type loss **
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
To achieve this we use the keyword 'super' to call the superclass constructor.
during initialisation how do we pass values from a subclass to its superclass in Java?
55
What are the reasons that make it bad practice to let the fields be inherited and not used in a class?
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
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
explain why the java compiler experiences type loss
57
What is a **polymorphic variable** in Java?
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
name 3 methods provided by the Object class in Java?
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
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.
What is the **benefit of using polymorphic variables** in Java?
60
this is inflexible because changes must be made to every piece of duplicate code if a change is made.
How is **code duplication ** inflexible?
61
What are the disadvantages of **code duplication?**
The disadvantages of this include: 1. inflexibility 2. error-proneness 3. waste of time
62
In what order should the **super keyword ** be used in a Java subclass constructor?
this should be used as the first statement within a Java subclass constructor.
63
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
using the following code create a scenario where the compiler would experience type loss ``` Vehicle v; Car c = new Car(); ```
64
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 is **code duplication** a waste of time?
65
What is **UML**
this stands for Unified Modeling Language, and it is a visual modeling language used for software engineering.
66
these can't be called by name in Java because their main purpose is to initialize objects and not to be used like methods.
Why can't **constructors** be called by name in Java?
67
this stands for Unified Modeling Language, and it is a visual modeling language used for software engineering.
What is **UML**
68
What is the **Object class** in Java?
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
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
What is a **subtype** in Java?
70
In Java, this keyword is used to call a superclass constructor from a subclass constructor, to properly initialize the superclass.
What does the **super** keyword do in Java?
71
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 is **code duplication** error-prone?
72
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.
What are the two purposes of **having a common superclass of all classes** in Java?
73
1.Sufficient memory for the new object is allocated. 2.The object’s fields are given default ‘zero’ values: Reference type fields are assigned null, numerical type primitive fields are assigned 0, floating point primitives are assigned 0.0 and boolean fields are assigned false. A char type field is also assigned a value of 0 that 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, using this(). 4.A parent class constructor is called (except for class Object), either explicitly using super() 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.
describe the 6 steps that take place when an object is initialised
74
when this is used within a constructor it will search for a constructor in the same class with a matching signature
describe the **this() keyword**
75
this keyword in Java is used to specify that a class is inheriting from a superclass.
What does the **'extends' keyword** in Java do?
76
[Chapter 10 glossary quiz](https://learn2.open.ac.uk/mod/quiz/view.php?id=2014931)
ignore
77
Do access modifiers of superclass members apply to subclasses in Java?
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
When might it be necessary to **refactor an inheritance hierarchy ** in Java?
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
What is the **benefit of using inheritance in Java?**
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
What is **casting ** in Java?
this in Java is the conversion of one type to another type using the cast operator.
81
What is the **benefit of using polymorphic variables** in Java?
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
Can you provide an example of how polymorphic variables can be used in Java?
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
give 2 reasons as to why it is good style to explicitly call the superclass constructor in a Java subclass?
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
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
describe **type loss **
85
Are constructors considered as members of a class in Java?
No, constructors are not considered as members of a class in Java.
86
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.
What is a **ClassCastException ** in Java?
87
Do private fields and methods get inherited by subclasses in Java?
No, private fields and methods are not inherited by subclasses in Java and hence will not be members of any subclasses.
88
Why can't **constructors** be called by name in Java?
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
How can a subclass access private fields of its superclass in Java?
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
What is the advantage of separating the responsibilities of constructors from methods in Java?
Separating the responsibilities of constructors from methods in Java allows for predictable initialization of objects.
91
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.
In Java, when can substitution be used for formal and actual parameters?
92
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
What is the **benefit of using inheritance in Java?**
93
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
What is the **Object class** in Java?
94
this is achieved by making a call from one constructor to another constructor using the this() keyword
how do we actually **reuse constructor code**
95
What is a **supertype** in Java?
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
explain why the java compiler experiences type loss
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
What does the concept of **substitution ** refer to in Java?
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
when would it be wise to **reuse constructor code**
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
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.
from the following classes create an example **Inheritance hierarchy** Classes: animal, mammal, dog, cat, poodle, dalmation
100
This keyword is used in the header of a subclass to specify that it is extending a superclass example: "public class MessagePost extends Post".
How is the **extends keyword ** used in Java?
101
What are the two purposes of **having a common superclass of all classes** in Java?
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
what are 4 advantages that come from using **inheritance**
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
how do we actually **reuse constructor code**
this is achieved by making a call from one constructor to another constructor using the this() keyword
104
this is sometimes necessary when the compiler experiences what is known as "type loss"
When might casting be necessary in Java?
105
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
What is a **class diagram** used for and what information will it illustrate?