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
T/F - A subclass inherits its superclass’ realization relationships
True
What is a functional interface in Java and what is its significant in Java SE 8?
Functional interface = interface that contains only 1 abstract method. Significant in Java SE 8 because it allows the use of “lambda” expressions
Give 3 examples of functional interfaces
ActionListener, Comparator, Runnable
This functional interface defines a method that is called when a user clicks a GUI button
ActionListener
This functional interface defines a method that can compare 2 objects of a given type (can be useful for sorting and ordering)
Comparator
This functional interface defines tasks that can be executed concurrently with other parts of the program, often used in multi-threaded applications
Runnable
What is the purpose of the default keyword in interface methods?
A method has a default implementation in case it is not overridden in the implementing class. Allows for backward compatibility when new methods are added to an interface
“Program to an interface, not an implementation” means what?
Write code that relies on interfaces rather than concrete implementations of classes
Interface inheritance vs implementation inheritance
Interface inheritance = you must implement an interfaces abstract methods
implementation inheritance = extending classes, super/subclasses
Althought interface inheritance requires more work than implementation inheritance, which one is more flexible?
Interface inheritance, as it eliminates tight coupling between classes
When you use a varaible of an interface type, you can assign it an object of any type that implements the interface ________ or _________
directly or indirectly
______ _______ are a good example of how interfaces enable systems to be modified easily
Device drivers
What type of relationship do subclass objects have with the superclass?
An “is-a” relationship