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.
to accomplish this:
Import java.util.Collections
what is the statement to
import the Collections class
describe an
abstract method
These are methods that have a header but no body.
That is, they have an interface but no implementation.
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?
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.
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.
can
interfaces
contain constructors
no, these do not contain constructors
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
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 is the Comparable interface implemented in Java?
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
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.
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.
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.
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?
refers to how easy the code is to keep code functional and adapt to changes.
describe
maintainability
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<MyClass>
The type given in the Comparable interface will determine the type of object being compared.