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

A

True

17
Q

Can an abstract class have default and static methods?

A

Yes

18
Q

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

A

Final

19
Q

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

A

True

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

A

True

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
Q

T/F - A subclass inherits its superclass’ realization relationships

A

True

26
Q

What is a functional interface in Java and what is its significant in Java SE 8?

A

Functional interface = interface that contains only 1 abstract method. Significant in Java SE 8 because it allows the use of “lambda” expressions

27
Q

Give 3 examples of functional interfaces

A

ActionListener, Comparator, Runnable

28
Q

This functional interface defines a method that is called when a user clicks a GUI button

A

ActionListener

29
Q

This functional interface defines a method that can compare 2 objects of a given type (can be useful for sorting and ordering)

A

Comparator

30
Q

This functional interface defines tasks that can be executed concurrently with other parts of the program, often used in multi-threaded applications

A

Runnable

31
Q

What is the purpose of the default keyword in interface methods?

A

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
Q

“Program to an interface, not an implementation” means what?

A

Write code that relies on interfaces rather than concrete implementations of classes

33
Q

Interface inheritance vs implementation inheritance

A

Interface inheritance = you must implement an interfaces abstract methods

implementation inheritance = extending classes, super/subclasses

34
Q

Althought interface inheritance requires more work than implementation inheritance, which one is more flexible?

A

Interface inheritance, as it eliminates tight coupling between classes

35
Q

When you use a varaible of an interface type, you can assign it an object of any type that implements the interface ________ or _________

A

directly or indirectly

36
Q

______ _______ are a good example of how interfaces enable systems to be modified easily

A

Device drivers

37
Q

What type of relationship do subclass objects have with the superclass?

A

An “is-a” relationship