Chapter 12 - Further abstraction techniques Flashcards

1
Q

We may create these within abstract classes or interfaces.

in cases where it may be impossible to define the implementation of a method because it is solely or heavily dependent upon its subclasses.

A

where would we
Use abstract methods

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

This is a further abstraction technique in Java that often contains only abstract method definitions and no implementations.

A

What is an
interface?

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

describe
Re-factoring

A

refers to improving the structure of the code while keeping the functionality the same

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

What are 3 advantages of using the @Override annotation when overriding a method of a superclass or implementing an interface in Java?

A

advantages include:
1. It states our intention that we are indeed meant to override a superclass method or implement an abstract method from an interface.
2. If we create an overridden method with an incorrect signature that does not match that of a method in the superclass or interface, the Java compiler will tell us that “method does not override or implement a method from a supertype,” allowing us to catch our error at compile time.
3. If a method is overridden by many subclasses, and we decide to change its header in the base superclass or interface, locating the subclasses that have overridden this method is easy, as the compiler will flag the places where the header does not match.

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

this type of class is appropriate if we have a collection of classes that are closely related.

For example:
1. BirthdayCard
2. ChristmasCard
3. GreetingCard

could extend the abstract class Card.

A

In general when would an abstract class be appropriate?

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

advantages include:
1. It states our intention that we are indeed meant to override a superclass method or implement an abstract method from an interface.
2. If we create an overridden method with an incorrect signature that does not match that of a method in the superclass or interface, the Java compiler will tell us that “method does not override or implement a method from a supertype,” allowing us to catch our error at compile time.
3. If a method is overridden by many subclasses, and we decide to change its header in the base superclass or interface, locating the subclasses that have overridden this method is easy, as the compiler will flag the places where the header does not match.

A

What are 3 advantages of using the @Override annotation when overriding a method of a superclass or implementing an interface in Java?

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

What is a question to ask when deciding whether to use an interface or an abstract class?

A

“Does this type need concrete elements such as instance fields, constructors, and method bodies?”

If yes, then an abstract class would be appropriate because interfaces cannot have instance fields or constructors.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q
public int compareTo(Pet otherPet)
{
    return (int) (weightInGrams() - otherPet.weightInGrams());
}
A

Can you provide an example implementation of the Comparable interface method compareTo()

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

What is inherited when we extend
abstract classes?

A

When we extend these types of classes, we inherit the type and a partial implementation.

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

The compareTo() method is used to compare two objects and determine their order.

It returns an int value that indicates whether one object comes before or after or is at the same level as another object.

A

How is the compareTo() method of the Comparable interface used in Java?

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

Three benefits of Inheritance and Interfaces

A

Inheritance:
1. less code duplication - due to the inheritance of fields and methods
2. polymorphic variables and method calls - where the dynamic type of a variable at runtime is what determines the behavior

Although interfaces do not help gain the first benefit, they can be used to gain the benefit of polymorphic variables by using them as a variable type (static type).

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

How is the compareTo() method of the Comparable interface used in Java?

A

The compareTo() method is used to compare two objects and determine their order.

It returns an int value that indicates whether one object comes before or after or is at the same level as another object.

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

In general when would an abstract class be appropriate?

A

this type of class is appropriate if we have a collection of classes that are closely related.

For example:
1. BirthdayCard
2. ChristmasCard
3. GreetingCard

could extend the abstract class Card.

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

To declare this, it is prefixed with the keyword “abstract” and has no body. Instead, it is terminated with a semicolon.

For example:
abstract public void methodName(params);

A

write a
declaration of an abstract method

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

Some languages allow for multiple inheritance, while others do not. Java allows for a limited use of multiple inheritance through interfaces.

A

Does java allow for multiple inheritance?

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

What are the possible int values returned by the compareTo() method in Java?

A

This method of Comparable interface can return one of three int values:
1. a negative value if this object comes before the second
2. a positive value if the second object comes before this object
3. and zero if both objects belong in the same position.

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

this can be accomplished with:
interfaceName.super.methodName(…)

A

how do we
call a default method of an interface

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

This is a class that typically has no implementation but states what any implementations of the specification must do.

In Java, an interface does just this by defining abstract methods.

A

What is a specification in Java?

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

Can you provide an example implementation of the Comparable interface method compareTo()

A
public int compareTo(Pet otherPet)
{
    return (int) (weightInGrams() - otherPet.weightInGrams());
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

No, these do not have instance fields

A

can
interfaces
contain instance fields

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

What is the purpose of abstract classes with abstract methods?

A

They force subclasses to implement the abstract methods, ensuring consistency across different subclass implementations.

For an abstract subclass to become a concrete class, it must implement all inherited abstract methods.

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

in this scenario it may be wiser to opt for an interface as Subtypes are not then bound to a single supertype as is the case with abstract classes.

This leads to less coupling and greater code flexibility and room for extensibility.

A

Why might it be wiser to opt for an interface instead of an abstract class if instance fields or constructors are not required?

note that either could be used

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q
  1. To implement multiple inheritance
  2. To use the interface as a supertype and the highest abstraction with no implementation - We want to treat objects that are instances of a number of different classes in a uniform way
  3. To implement a specification - We want to allow each of those concrete classes to decide how they should implement the specification. We only define an action that concrete classes should have not the actual behaviour
A

What are three motivations for creating an interface?

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

What is inherited when we extend
concrete classes

A

When we extend this type of class, we inherit all implementation of the class as well as its type.

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

to accomplish this:

Import java.util.Collections

A

what is the statement to
import the Collections class

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

describe an
abstract method

A

These are methods that have a header but no body.

That is, they have an interface but no implementation.

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

This class in Java is a utility class that provides many methods for manipulating the contents of collections, such as lists and sets.

A

What is the Collections class in Java?

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

What are the
general benefits of inheritance?

A

The general benefits of this are:
1. code inheritance -where we may inherit common code such as fields and methods
2. subtyping - where we may inherit the type of the superclass which allows for polymorphism and specialization of a type.

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

describe the solution where we have two classes with identical methods except the data they use comes from thier class constant

A

the solution here is:
1. Keep the class constant in the subclass
2. Redefine the method in the superclass
3. Use an abstract getter method in the superclass and a concrete getter method in the subclass to access the subclass class constant.

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

can
interfaces
contain constructors

A

no, these do not contain constructors

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

a method with either of these keywords within an interface must have a body with an implementation

A

within an interface methods with the static or default kewyord must have what

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

The Comparable interface in Java can be implemented by other classes using the syntax

public class MyClass implements Comparable<MyClass>

The type given in the Comparable interface will determine the type of object being compared.

A

How is the Comparable interface implemented in Java?

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

When we extend this type of class, we inherit all implementation of the class as well as its type.

A

What is inherited when we extend
concrete classes

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

Does java allow for multiple inheritance?

A

Some languages allow for multiple inheritance, while others do not. Java allows for a limited use of multiple inheritance through interfaces.

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

How does an interface differ from an abstract class?

A

While both are abstractions, an interface has its own set of rules and a class can implement multiple interfaces, whereas it can only extend one abstract class.

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

What is the
Comparable interface
in Java?

A

This is used when we have objects that can be sorted in some way and has one method called compareTo() that returns an int value indicating whether one object belongs before or after or at the same level as another object.

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

This method of Comparable interface can return one of three int values:
1. a negative value if this object comes before the second
2. a positive value if the second object comes before this object
3. and zero if both objects belong in the same position.

A

What are the possible int values returned by the compareTo() method in Java?

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

refers to how easy the code is to keep code functional and adapt to changes.

A

describe
maintainability

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

How is the Comparable interface implemented in Java?

A

The Comparable interface in Java can be implemented by other classes using the syntax

public class MyClass implements Comparable<MyClass>

The type given in the Comparable interface will determine the type of object being compared.

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

This is when a class inherits directly from two or more superclasses.

A

What is
multiple inheritance?

41
Q

Can a class without abstract methods be declared as abstract?

A

Yes, a class without abstract methods may be declared as abstract.

This allows us to prevent the instantiation of the superclass, only allowing its subclasses to be instantiated.

For example, we would not have an instance of a vehicle, but rather we would only want to instantiate subclasses of vehicle.

42
Q

within an interface methods with the static or default kewyord must have what

A

a method with either of these keywords within an interface must have a body with an implementation

43
Q

What are default methods in Java interfaces?

A

these were added in Java 8 to allow interfaces to have implementation for methods.

When a method is defined using this keyword in an interface, it will have an implementation and that implementation will be inherited by all classes that implement or inherit from the interface.

It’s important to note that the functionality of this method type is rather limited, as the interface has no constructor or instance fields and so there is no state that can be examined or manipulated by the default method.

In general, interfaces will be limited to using only abstract methods, but these can be useful in certain cases.

44
Q

this is appropriate if we have a collection of classes from different hierarchies that are not closely related but share some common characteristic.

For example, we might have an interface Sendable that can be implemented by cards, letters, parcels, emails, furniture, and food boxes.

They can all be sent, but how they are sent depends solely on the concrete implementation.

A

in general, when would an interface be appropriate?

45
Q

When we implement these, we usually only inherit the type and no implementations

A

What is inherited when we implement
interfaces

46
Q

Why might it be wiser to opt for an interface instead of an abstract class if instance fields or constructors are not required?

note that either could be used

A

in this scenario it may be wiser to opt for an interface as Subtypes are not then bound to a single supertype as is the case with abstract classes.

This leads to less coupling and greater code flexibility and room for extensibility.

47
Q

in general, when would an interface be appropriate?

A

this is appropriate if we have a collection of classes from different hierarchies that are not closely related but share some common characteristic.

For example, we might have an interface Sendable that can be implemented by cards, letters, parcels, emails, furniture, and food boxes.

They can all be sent, but how they are sent depends solely on the concrete implementation.

48
Q

describe
extensibility

A

refers to how easy it is to add new features to the code

49
Q

What happens if a subclass of an abstract class does not provide concrete implementations?

A

If a subclass of an abstract class does not provide concrete implementations, then it itself will become an abstract class (an abstract subclass).

50
Q

When might we define an interface instead of using an abstract class?

A

When there is a common characteristic between classes but implementing through inheritance such as an abstract class makes no sense since the classes may only be related through this single characteristic and nothing else. For example:

The class is weighable (the concrete classes are weighable)
The class is drawable (the concrete classes are drawable)
The class is an actor (the concrete classes are actors)

51
Q

where would we
Use abstract methods

A

We may create these within abstract classes or interfaces.

in cases where it may be impossible to define the implementation of a method because it is solely or heavily dependent upon its subclasses.

52
Q

What happens when
a class is defined as abstract?

A

No instances of that class can be created, and calling the new keyword on this class will result in a compile-time error.

53
Q

describe an
astract class

A

These are classes that are not intended to use for creating instances of the class. Their purpose is to serve as a superclass for subclasses.

54
Q

This is used when we have objects that can be sorted in some way and has one method called compareTo() that returns an int value indicating whether one object belongs before or after or at the same level as another object.

A

What is the
Comparable interface
in Java?

55
Q

What is inherited when we implement
interfaces

A

When we implement these, we usually only inherit the type and no implementations

56
Q

“Does this type need concrete elements such as instance fields, constructors, and method bodies?”

If yes, then an abstract class would be appropriate because interfaces cannot have instance fields or constructors.

A

What is a question to ask when deciding whether to use an interface or an abstract class?

57
Q

what is
Computational modelling

A

This is the act of modelling or simulating complex or real world systems within a simulation

58
Q

all methods within this:
1. abstract
2. concrete
3. static

will have public visibility, therefore the public keyword may be omitted

A

what visibility do all methods have within an interface

59
Q

How is the sort() method used in the Collections class?

A

To use the sort() method in the Collections class, you simply call the method on the collection you want to sort, like this:

Collections.sort(aCollection);

Note:
The sort() method in the Collections class can be used to sort any collection that implements the Comparable interface.

60
Q

Can concrete classes have abstract methods?

A

No, allowing an abstract method within a concrete class would mean that we would not be able to create an instance of that class since it lacks an implementation of the method.

61
Q

describe
maintainability

A

refers to how easy the code is to keep code functional and adapt to changes.

62
Q

What happens when
a subclass does not implement all inherited abstract methods?

A

The subclass itself becomes an abstract class (abstract subclass), and we will not be able to create an instance of that class since it does not have an implementation for inherited abstract methods.

63
Q

can
interfaces
contain instance fields

A

No, these do not have instance fields

64
Q

What is an
interface?

A

This is a further abstraction technique in Java that often contains only abstract method definitions and no implementations.

65
Q

refers to how easy it is to add new features to the code

A

describe
extensibility

66
Q

What is
multiple inheritance?

A

This is when a class inherits directly from two or more superclasses.

67
Q

within an interface does the abstract keyword have to be given with the abstract method

A

no, within these Abstract methods do not have to contain the keyword abstract

68
Q

What is a specification in Java?

A

This is a class that typically has no implementation but states what any implementations of the specification must do.

In Java, an interface does just this by defining abstract methods.

69
Q

What is the Collections class in Java?

A

This class in Java is a utility class that provides many methods for manipulating the contents of collections, such as lists and sets.

70
Q

These are classes that are not intended to use for creating instances of the class. Their purpose is to serve as a superclass for subclasses.

A

describe an
astract class

71
Q

Why can’t fields in a superclass be overridden?

A

Fields in a superclass cannot be overridden because they are not methods. As a result, we cannot set or modify the subclass constant in the superclass.

72
Q

Inheritance:
1. less code duplication - due to the inheritance of fields and methods
2. polymorphic variables and method calls - where the dynamic type of a variable at runtime is what determines the behavior

Although interfaces do not help gain the first benefit, they can be used to gain the benefit of polymorphic variables by using them as a variable type (static type).

A

Three benefits of Inheritance and Interfaces

73
Q

What are three motivations for creating an interface?

A
  1. To implement multiple inheritance
  2. To use the interface as a supertype and the highest abstraction with no implementation - We want to treat objects that are instances of a number of different classes in a uniform way
  3. To implement a specification - We want to allow each of those concrete classes to decide how they should implement the specification. We only define an action that concrete classes should have not the actual behaviour
74
Q

in this case the class must override the method, as it must be clear which method should be called

the implemnting class can:
1.Override the default method and then call the preferred implementation
2.Override the default method and create its own implementation

A

what action must be taken if a class inherits from two different interfaces a default method with a matching signature

75
Q

In an inheritance hierarchy, a class inherits from all the classes of its superclass hierarchy, while in multiple inheritance, a class inherits directly from multiple superclasses.

A

How does multiple inheritance differ from inheritance hierarchy?

76
Q

The general benefits of this are:
1. code inheritance -where we may inherit common code such as fields and methods
2. subtyping - where we may inherit the type of the superclass which allows for polymorphism and specialization of a type.

A

What are the
general benefits of inheritance?

77
Q

describe a
concrete class

A

These are classes that must have implementations of all their methods, whereas an abstract class may omit the implementation.

78
Q

what action must be taken if a class inherits from two different interfaces a default method with a matching signature

A

in this case the class must override the method, as it must be clear which method should be called

the implemnting class can:
1.Override the default method and then call the preferred implementation
2.Override the default method and create its own implementation

79
A

ignore

80
Q

To use the sort() method in the Collections class, you simply call the method on the collection you want to sort, like this:

Collections.sort(aCollection);

Note:
The sort() method in the Collections class can be used to sort any collection that implements the Comparable interface.

A

How is the sort() method used in the Collections class?

81
Q

When there is a common characteristic between classes but implementing through inheritance such as an abstract class makes no sense since the classes may only be related through this single characteristic and nothing else. For example:

The class is weighable (the concrete classes are weighable)
The class is drawable (the concrete classes are drawable)
The class is an actor (the concrete classes are actors)

A

When might we define an interface instead of using an abstract class?

82
Q

These are methods that have a header but no body.

That is, they have an interface but no implementation.

A

describe an
abstract method

83
Q

What are the differences between
an interface, an abstract class, and a concrete class in Java?

A
  1. An interface would most likely only contain definitions of methods and contains no implementation for its subclasses.
  2. An abstract class would contain partial implementation for its subclasses, such as fields and methods that can be inherited. Inherited abstract methods might also be implemented here.
  3. A concrete class will be a full implementation and will have implementations for any abstract methods that have been inherited.
84
Q

no, these do not contain constructors

A

can
interfaces
contain constructors

85
Q

This is the act of modelling or simulating complex or real world systems within a simulation

A

what is
Computational modelling

86
Q

what visibility do all methods have within an interface

A

all methods within this:
1. abstract
2. concrete
3. static

will have public visibility, therefore the public keyword may be omitted

87
Q

These are classes that must have implementations of all their methods, whereas an abstract class may omit the implementation.

A

describe a
concrete class

88
Q

no, within these Abstract methods do not have to contain the keyword abstract

A

within an interface does the abstract keyword have to be given with the abstract method

89
Q

what is the statement to
import the Collections class

A

to accomplish this:

Import java.util.Collections

90
Q

refers to improving the structure of the code while keeping the functionality the same

A

describe
Re-factoring

91
Q

which type of field is allowed within an interface

A

this may contain fields that are:
class constants with public visibility

therefore the
public static final keywords
are assumed automatically and may be omitted

92
A

ignore

93
Q

these were added in Java 8 to allow interfaces to have implementation for methods.

When a method is defined using this keyword in an interface, it will have an implementation and that implementation will be inherited by all classes that implement or inherit from the interface.

It’s important to note that the functionality of this method type is rather limited, as the interface has no constructor or instance fields and so there is no state that can be examined or manipulated by the default method.

In general, interfaces will be limited to using only abstract methods, but these can be useful in certain cases.

A

What are default methods in Java interfaces?

94
Q

this may contain fields that are:
class constants with public visibility

therefore the
public static final keywords
are assumed automatically and may be omitted

A

which type of field is allowed within an interface

95
Q

write a
declaration of an abstract method

A

To declare this, it is prefixed with the keyword “abstract” and has no body. Instead, it is terminated with a semicolon.

For example:
abstract public void methodName(params);

96
Q

When we extend these types of classes, we inherit the type and a partial implementation.

A

What is inherited when we extend
abstract classes?

97
Q

How does multiple inheritance differ from inheritance hierarchy?

A

In an inheritance hierarchy, a class inherits from all the classes of its superclass hierarchy, while in multiple inheritance, a class inherits directly from multiple superclasses.

98
Q

How do you declare and implement an interface in Java?

A

To declare an interface, you use the header

public interface InterfaceName
{
}

To implement an interface, you use the “implements” clause in the class header, such as

public class Fox extends Animal implements Drawable
{
}
99
Q

how do we
call a default method of an interface

A

this can be accomplished with:
interfaceName.super.methodName(…)