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