Interfaces Flashcards

1
Q

What access modifiers do interfaces accept?

A

Only package and public

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

Why interfaces don’t have protected methods?

A

Because they are not extended by classes

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

Why interfaces don’t have package access methods?

A

Legacy reasons

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

Can interfaces have concrete methods?

A

Yes - default, static, private, private static

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

When overriding an abstract method, what are rules for return type?

A

Return type must be the same, or covariant

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

How do interfaces differ from abstract classes?

A

Interfaces have implicit modifiers, abstract classes do not

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

What is implicit regarding interfaces variables?

A

They are “public final”

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

What is implicit regarding interfaces?

A

They are abstract

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

What is implicit regarding interfaces methods?

A

Methods without bodies are “abstract”
Methods without “private” access modifier are “public” - if something else is defined -> compile error

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

What if different interfaces try to implement methods with same signatures?

A

Compile error because compiler doesn’t know which method to override or implement
eg.
interface A -> void int a()
interface B -> int eat()

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

What are methods inside interface?

A

They are abstract and public

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

What attributes do static methods inside interface have?

A

They are public, and cannot be abstract or final

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

How do we run static methods in interfaces?

A

Via interface name -> Interface.method()

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

Can default methods be overridden?

A

Yes

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

How can we access overridden (or hidden) default method from implemented interface?

A

InterfaceName.super.method()

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

Can static methods be overridden?

A

No

17
Q

What if two different interfaces that are being implemented have same default method?

A

Then that method needs to be implemented in class

18
Q

What if two different interfaces that are being implemented have default method with same signature, but different return type?

A

Compiler error, since it doesn’t know which method to implement, and both of them need to be implemented.

19
Q

What if two different interfaces that are being implemented have method with same signature, but different return type?

A

Compiler error, since it doesn’t know which method to implement, and both of them need to be implemented.

20
Q

What if two different interfaces that are being implemented don’t have same default methods?

A

Then none of default methods don’t need to be implemented in class

21
Q

What are rules for default methods?

A
  • Can be overridden
  • Cannot be static, final, abstract
  • Must have a method body
  • Implicitly public, cannot be private or protected
22
Q

What do we need to watch out when implementing multiple interfaces?

A

For double methods