Week 10 Flashcards
What is an interface in Java and what does it contain?
A collection of abstract methods and constants, it does not contain any method body’s
T/F - All methods in an interface are abstract by default
True
What is an interface in Java and what does it contain?
No, an interface cannot be instantiated in Java
What is the visibility of methods in an interface by default?
Public
How does a class formally implement an interface in Java?
By stating “implements” in the class header and implementing each method
What happens when a class states that it implements an interface?
It must define all the methods in the interface, or there will be a compilation error
Can an interface contain constants? How do they work in a class that implements the interface?
Yes. When class implements an interface it gains access to all the constants defined in that interface
Why would a class implement multiple interfaces? How would the class do this?
class MyClass implements interface1, interface 2
All objects of a class that implements multiple interfaces have what type fo a relationship?
An is-a relationship with each implemented interface type
4 Benefits of interfaces?
Help achieve abstraciton
Support dynamic method resolution (at runtime)
Achieve loose coupling
Separate definition of a method from the inheritance hierarchy
How are interfaces different from classes?
They contain only constants and abstract methods AND all interface members are implicitly public
How can interfaces be used for polymorphism?
2 classes implement an interface = Allow objects of unrelated classes to be processed polymorphically, they respond to the same method calls
Interface vs abstract class
Interface can only contain constants and abstract methods, an abstract class can contain concrete methods and instance variables
T/F - In an abstract class access modifiers do not need to be explicitly defined
False
T/F - Interfaces standardize operations, objects figure out how to implement them
True
T/F - an interface must be declared in a file with the same name as the interface, and a .java filename extension
True
Can an abstract class have default and static methods?
Yes
In an interface all variables declared are by default _____, an abstract class does not have this restriction
Final
T/F - An abstract class can have final, non-final, static and non-static varaibles
True
T/F - an interface has final, non-final, static and non-static variables
False. Interfaces only have static and final
Can an interface extend another interface? How about an abstract class?
Yes. An abstract class can extend another class and implement multiple interfaces?
T/F - When naming interface methods, you should choose a general name for it, as many unrelated classes may use the same method when implemented
True
In a UML, what is special about an interface?
«interface» will be above the interface name
How does UML express the relationship between a class and an interface? What does this look like?
Through realization. It’s a dashed arrow with a hollow arrowhead, point from the implemented class to the interface
The class is said to “realize” methods of the interface