II.
Interfaces
- Definition:
- A group or collection of method definitions and constant
values.
- Keyword: implements - allows
a class to realize the interface
- Purpose/Benefits:
- Capturing similarities among unrelated classes without
artificially forcing a class relationship.
- Declaring methods that one or more classes are expected
to implement.
- Revealing an object's programming interface without
revealing its class.
- Keys
- When a class realizes an interface, it agrees to follow
the interfaces behavior.
- Therefore, it must have all of the methods in the
interface in the realizing class.
- Holds a constant protocol of method definitions
- A class can implement as many interfaces as you want.
III. Multiple Choice
- Which of the following would best serve as the interface to the
other items listed?
a. Washing Machine
b. Refridgerator
c. MicrowaveOven
d. Appliance
e. Dryer
- Given the following class definition:
public class Vehicle
{
public void drive(double miles)
{
...
}
}
Why won't this subclass compile?
public class Car extends Vehicle
{
private void drive(double miles)
{
...
}
}
A) It will compile.
B) The drive method of the Car class can not have a different return
type.
C) A method can never be declared private.
D) Car can not have a method with the same name as one in its
superclass.
E) A method must have a different parameter name.
Given the following Java code:
<><>
interface Animal {
public void speak();
}
class Canine implements Animal {
public void speak() {
System.out.print("Canine:");
}
public static void main(String[] args) {
Animal a = null;
Canine c = null;
// What could go here?
}
}A) c = a;
B) a = c;
C) c= new Canine();
D) B and C
E) A and C
- Given that
the Employee class extends Person class and this segment of code:
Employee boss = new Employee();
Person mom = new Person();
Object anObject;
Person tom;
Which of the following assignments are valid?
A) boss = mom;
B) anObject = boss;
C) mom = boss;
D) B and C
E) A, B, and C
- Given the following code segments:
public class Country implements Locatable
{
....
}
public class Tester
{
....
Country norway = new Country();
statement2
}
Which of the following statemens would be valid as statement2?
A) Locatable where = norway;
B) Locatable where = new Locatable();
C) Locatable where = Country();
D) Locatable where = new York();
E) Country where = new Locatable();
Free Response Question
An airport needs a program to document the different
flights leaving
Given the following class:
BankAccount
a.) Write a CheckingAccount class
that is a subclass of BankAccount that charges transaction fees per
transactions, which is entered by the user. You should modify the
methods as necessary, add any necessary local variables, and add a
deductFees( ) method that deducts the fees for the number of
transactions and then resets the transaction count.
b.) Write a SavingsAccount class that is a subclass of BankAccount that
earns interest at a fixed rate and has an addInterest( ) method allow
the account to earn interest in the account balance. You may use any of
the methods added or modified in part A. Assume that the deductFees( )
method works as it should regardless of you answer in part A.
Answers:
1. D
2. B
3. D
4. D
5. A
FRQ Answers
A.)
CheckingAccount
B.)
SavingsAccount