Java Interfaces Flashcards

(15 cards)

1
Q

How do interfaces help with inheritance?

A

Since multiple inheritance is not possible in Java, using interfaces can implement methods that aren’t hierarchially related.

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

Give the format of an interface called “Measurable” that returns a double named “getMeasure” with no parameters.

A

public interface Measurable {
double getMeasure();
}

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

How would class “This” implement the class “Measurable”?

A

public class This implements Measurable{}

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

Can you use interfaces like classes?

A

Yes you can pass interfaces that will take variables of type Measurable, which just takes instances of classes that implements Measurable.

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

What’s the difference between “abstract” and an interface?

A

Classes which extend an abstract are related through hierarchy, interfaces can be completely unrelated (also can implement multiple interfaces).

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

Using interfaces, how can I ensure a Node will be printable and comparable?

A

interface ListItem extends Comparable, Printable {}. Keep in mind interfaces are merely things that a class has to satisfy.

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

What is the protection of Interfaces?

A

All methods are public in interfaces, and must be public in the class which implements them as well.

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

Can you have instances of an interface?

A

No, interfaces are just a collection of methods, but polymorphism works for classes implementing an interface.

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

What are default methods?

A

Ways to update old interfaces which is a part of an interface that has code! The code can be redeclared, overriden, or left unchanged.

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

Whats the point of a default method?

A

To add a method to keep old code working (e.g. interface was created a while ago and lots of classes implement it)

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

What happens if a class implements 2 interfaces that contain the same method name and parameters?

A

cant happen, compile-time error (we can fix this by defining a new behaviour with the same type though)

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

What happens when a subclass has a parent and interface with the same method?

A

The subclass uses the superclass version, extends has priority

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

Can interfaces have static methods?

A

Yes, and referred to by the interface:
e.g. int1.staticMethod(…);

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

Can you have an interface inside another?

A

Yes, called nested interfaces
e.g.:
interface Printable {
void print();
interface Testable {
void test();
}}

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

How do I refer to a nested interface?

A

Specify the name of the outer interface, followed by a dot:
class Test implements Printable.Testable

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