Interfaces Flashcards

1
Q

Interface

A

an interface is used as a contract to guarantee implementation of certain behaviors in a subclass.It’s an abstract entity - all methods are implicitly “public abstract” while instance variables, if there are any, are “public static final”.

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

To implement an interface use

A

the implement keyword

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

Interface methods and variables

A
  • All methods are public abstract

- all variables are public static final

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

Interfaces and Inheritance

A
  • said to have “type” inheritance
  • a class that implements an interface is polymorphically considered an instance of the interface
  • An interface describes what behaviors a class should have but provides none of its own.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Class and interfaces

A

-A class can implement multiple interfaces but can only extend one class

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

Java 8 Interfaces

A

Public-facing interfaces (APIs) were difficult to update or change. So default methods were created.

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

Default methods

A
-interface methods that are implemented in the interface
(Because an implementation is present in the interface, classes that implement that interface are not required to implement the method. To use a default method, you just add the default keyword before the method declaration in the interface.Then you can provide an implementation, like normal.) 
-implementing classes are not required to implement default methods, but they can override them
-the guarantee with an interface is that the implementing class will provide an implementation for those other methods, so they are safe to call.Interfaces are also allowed to have static methods with definitions.Like any other static method, you can invoke these without creating an instance of a class that implements the interface.And because they are static methods, they can only call other static methods, or access methods through local reference variable
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

static methods

A
  • can be called without creating an instance of the class

- can only call other static methods directly. Must use local reference variables to call non-static methods

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

Multiple Inheritance

A

Use the super() keyword to refer to a specific interface method (in the case that two interfaces have default methods with the same name)

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

Abstract Classes

A
  • Can have instance variables that are not always public, static, final
  • can have methods that are bot public
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is the difference between interfaces vs abstract classes?*

A
  • A class can extend only one class (abstract or not) but can implement multiple interfaces
  • Interface variables are implicitly public static final. Abstract classes can declare any modifiers on instance variables.
  • Interfaces can (as of Java 8) provide default methods, but all others are public abstract. Abstract classes can declare any modifiers on their methods.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Why use an interface over an abstract class?

A
  • In general, if you wanted to develop and enforce a certain hierarchy of functionality where state and behavior was tracked, then you would use an abstract class
  • If you want a class to be free to implement functionality without constrained to a hierarchy, then an interface is a better approach.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly