Chapter 12 - Further abstraction techniques Flashcards
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.
where would we
Use abstract methods
This is a further abstraction technique in Java that often contains only abstract method definitions and no implementations.
What is an
interface?
describe
Re-factoring
refers to improving the structure of the code while keeping the functionality the same
What are 3 advantages of using the @Override annotation when overriding a method of a superclass or implementing an interface in Java?
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.
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.
In general when would an abstract class be appropriate?
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.
What are 3 advantages of using the @Override annotation when overriding a method of a superclass or implementing an interface in Java?
What is a question to ask when deciding whether to use an interface or an abstract class?
“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.
public int compareTo(Pet otherPet) { return (int) (weightInGrams() - otherPet.weightInGrams()); }
Can you provide an example implementation of the Comparable interface method compareTo()
What is inherited when we extend
abstract classes?
When we extend these types of classes, we inherit the type and a partial implementation.
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 is the compareTo() method of the Comparable interface used in Java?
Three benefits of Inheritance and Interfaces
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 is the compareTo() method of the Comparable interface used in Java?
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.
In general when would an abstract class be appropriate?
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.
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);
write a
declaration of an abstract method
Some languages allow for multiple inheritance, while others do not. Java allows for a limited use of multiple inheritance through interfaces.
Does java allow for multiple inheritance?
What are the possible int values returned by the compareTo() method in Java?
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.
this can be accomplished with:interfaceName.super.methodName(…)
how do we
call a default method of an interface
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.
What is a specification in Java?
Can you provide an example implementation of the Comparable interface method compareTo()
public int compareTo(Pet otherPet) { return (int) (weightInGrams() - otherPet.weightInGrams()); }
No, these do not have instance fields
can
interfaces
contain instance fields
What is the purpose of abstract classes with abstract methods?
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.
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.
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
- To implement multiple inheritance
- 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
- 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
What are three motivations for creating an interface?
What is inherited when we extend
concrete classes
When we extend this type of class, we inherit all implementation of the class as well as its type.