Week 10 Flashcards

1
Q

What is an interface in Java and what does it contain?

A

A collection of abstract methods and constants, it does not contain any method body’s

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

T/F - All methods in an interface are abstract by default

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is an interface in Java and what does it contain?

A

No, an interface cannot be instantiated in Java

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is the visibility of methods in an interface by default?

A

Public

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How does a class formally implement an interface in Java?

A

By stating “implements” in the class header and implementing each method

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What happens when a class states that it implements an interface?

A

It must define all the methods in the interface, or there will be a compilation error

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Can an interface contain constants? How do they work in a class that implements the interface?

A

Yes. When class implements an interface it gains access to all the constants defined in that interface

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Why would a class implement multiple interfaces? How would the class do this?

A

class MyClass implements interface1, interface 2

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

All objects of a class that implements multiple interfaces have what type fo a relationship?

A

An is-a relationship with each implemented interface type

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

4 Benefits of interfaces?

A

Help achieve abstraciton
Support dynamic method resolution (at runtime)
Achieve loose coupling
Separate definition of a method from the inheritance hierarchy

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How are interfaces different from classes?

A

They contain only constants and abstract methods AND all interface members are implicitly public

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How can interfaces be used for polymorphism?

A

2 classes implement an interface = Allow objects of unrelated classes to be processed polymorphically, they respond to the same method calls

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Interface vs abstract class

A

Interface can only contain constants and abstract methods, an abstract class can contain concrete methods and instance variables

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

T/F - In an abstract class access modifiers do not need to be explicitly defined

A

False

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

T/F - Interfaces standardize operations, objects figure out how to implement them

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

T/F - an interface must be declared in a file with the same name as the interface, and a .java filename extension

17
Q

Can an abstract class have default and static methods?

18
Q

In an interface all variables declared are by default _____, an abstract class does not have this restriction

19
Q

T/F - An abstract class can have final, non-final, static and non-static varaibles

20
Q

T/F - an interface has final, non-final, static and non-static variables

A

False. Interfaces only have static and final

21
Q

Can an interface extend another interface? How about an abstract class?

A

Yes. An abstract class can extend another class and implement multiple interfaces?

22
Q

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

23
Q

In a UML, what is special about an interface?

A

«interface» will be above the interface name

24
Q

How does UML express the relationship between a class and an interface? What does this look like?

A

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

25
T/F - A subclass inherits its superclass' realization relationships
True
26
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
27
Give 3 examples of functional interfaces
ActionListener, Comparator, Runnable
28
This functional interface defines a method that is called when a user clicks a GUI button
ActionListener
29
This functional interface defines a method that can compare 2 objects of a given type (can be useful for sorting and ordering)
Comparator
30
This functional interface defines tasks that can be executed concurrently with other parts of the program, often used in multi-threaded applications
Runnable
31
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
32
"Program to an interface, not an implementation" means what?
Write code that relies on interfaces rather than concrete implementations of classes
33
Interface inheritance vs implementation inheritance
Interface inheritance = you must implement an interfaces abstract methods implementation inheritance = extending classes, super/subclasses
34
Althought interface inheritance requires more work than implementation inheritance, which one is more flexible?
Interface inheritance, as it eliminates tight coupling between classes
35
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
36
______ _______ are a good example of how interfaces enable systems to be modified easily
Device drivers
37
What type of relationship do subclass objects have with the superclass?
An "is-a" relationship