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
to accomplish this: `Import java.util.Collections`
what is the statement to **import the Collections class**
26
describe an **abstract method**
These are methods that have a header but no body. That is, they have an interface but no implementation.
27
This class in Java is a utility class that provides many methods for manipulating the contents of collections, such as lists and sets.
What is the Collections class in Java?
28
What are the **general benefits of inheritance?**
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.
29
describe the solution where we have two classes with identical methods except the data they use comes from thier class constant
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.
30
can **interfaces** contain constructors
no, these do not contain constructors
31
a method with either of these keywords within an interface must have a body with an implementation
within an interface methods with the static or default kewyord must have what
32
The Comparable interface in Java can be implemented by other classes using the syntax `public class MyClass implements Comparable` The type given in the Comparable interface will determine the type of object being compared.
How is the Comparable interface implemented in Java?
33
When we extend this type of class, we inherit all implementation of the class as well as its type.
What is inherited when we extend **concrete classes**
34
Does java allow for multiple inheritance?
Some languages allow for multiple inheritance, while others do not. Java allows for a limited use of multiple inheritance through interfaces.
35
How does an interface differ from an abstract class?
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.
36
What is the **Comparable interface** in Java?
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.
37
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.
What are the possible int values returned by the compareTo() method in Java?
38
refers to how easy the code is to keep code functional and adapt to changes.
describe **maintainability**
39
How is the Comparable interface implemented in Java?
The Comparable interface in Java can be implemented by other classes using the syntax `public class MyClass implements Comparable` The type given in the Comparable interface will determine the type of object being compared.
40
This is when a class inherits directly from two or more superclasses.
What is **multiple inheritance?**
41
Can a class without abstract methods be declared as abstract?
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
within an interface methods with the static or default kewyord must have what
a method with either of these keywords within an interface must have a body with an implementation
43
What are **default methods** in Java interfaces?
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
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.
in general, when would an interface be appropriate?
45
When we implement these, we usually only inherit the type and no implementations
What is inherited when we implement **interfaces**
46
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
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
in general, when would an interface be appropriate?
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
describe **extensibility**
refers to how easy it is to add new features to the code
49
What happens if a subclass of an abstract class does not provide concrete implementations?
If a subclass of an abstract class does not provide concrete implementations, then it itself will become an abstract class (an abstract subclass).
50
When might we define an interface instead of using an abstract class?
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
where would we **Use abstract methods**
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
What happens when **a class is defined as abstract?**
No instances of that class can be created, and calling the new keyword on this class will result in a compile-time error.
53
describe an **astract class**
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
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.
What is the **Comparable interface** in Java?
55
What is inherited when we implement **interfaces**
When we implement these, we usually only inherit the type and no implementations
56
"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.
What is a question to ask when deciding whether to use an interface or an abstract class?
57
what is **Computational modelling**
This is the act of modelling or simulating complex or real world systems within a simulation
58
all methods within this: 1. abstract 2. concrete 3. static will have public visibility, therefore the public keyword may be omitted
what visibility do all methods have within an **interface**
59
How is the sort() method used in the Collections class?
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
Can concrete classes have abstract methods?
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
describe **maintainability**
refers to how easy the code is to keep code functional and adapt to changes.
62
What happens when **a subclass does not implement all inherited abstract methods?**
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
can **interfaces** contain instance fields
No, these do not have instance fields
64
What is an **interface?**
This is a further abstraction technique in Java that often contains only abstract method definitions and no implementations.
65
refers to how easy it is to add new features to the code
describe **extensibility**
66
What is **multiple inheritance?**
This is when a class inherits directly from two or more superclasses.
67
within an interface does the abstract keyword have to be given with the abstract method
no, within these Abstract methods do not have to contain the keyword abstract
68
What is a specification in Java?
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
What is the Collections class in Java?
This class in Java is a utility class that provides many methods for manipulating the contents of collections, such as lists and sets.
70
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.
describe an **astract class**
71
Why can't fields in a superclass be overridden?
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
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).
Three benefits of Inheritance and Interfaces
73
What are three motivations for creating an interface?
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
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
what action must be taken if a class inherits from two different interfaces a default method with a matching signature
75
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.
How does multiple inheritance differ from inheritance hierarchy?
76
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.
What are the **general benefits of inheritance?**
77
describe a **concrete class**
These are classes that must have implementations of all their methods, whereas an abstract class may omit the implementation.
78
what action must be taken if a class inherits from two different interfaces a default method with a matching signature
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
[Chapter 12 glossary quiz](https://learn2.open.ac.uk/mod/quiz/view.php?id=2014943)
ignore
80
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.
How is the sort() method used in the Collections class?
81
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)
When might we define an interface instead of using an abstract class?
82
These are methods that have a header but no body. That is, they have an interface but no implementation.
describe an **abstract method**
83
What are the differences between **an interface, an abstract class, and a concrete class in Java?**
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
no, these do not contain constructors
can **interfaces** contain constructors
85
This is the act of modelling or simulating complex or real world systems within a simulation
what is **Computational modelling**
86
what visibility do all methods have within an **interface**
all methods within this: 1. abstract 2. concrete 3. static will have public visibility, therefore the public keyword may be omitted
87
These are classes that must have implementations of all their methods, whereas an abstract class may omit the implementation.
describe a **concrete class**
88
no, within these Abstract methods do not have to contain the keyword abstract
within an interface does the abstract keyword have to be given with the abstract method
89
what is the statement to **import the Collections class**
to accomplish this: `Import java.util.Collections`
90
refers to improving the structure of the code while keeping the functionality the same
describe **Re-factoring**
91
which type of field is allowed within an interface
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
[Chapter 12 quiz](https://learn2.open.ac.uk/mod/quiz/view.php?id=2014945)
ignore
93
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.
What are **default methods** in Java interfaces?
94
this may contain fields that are: **class constants with public visibility** therefore the `public static final` keywords are assumed automatically and may be omitted
which type of field is allowed within an interface
95
write a **declaration of an abstract method**
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
When we extend these types of classes, we inherit the type and a partial implementation.
What is inherited when we extend **abstract classes?**
97
How does multiple inheritance differ from inheritance hierarchy?
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
How do you declare and implement an interface in Java?
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
how do we **call a default method of an interface**
this can be accomplished with: `interfaceName.super.methodName(…)`