interfaces Flashcards

1
Q

polymorphism

A

many forms (when code looks the same, but behavior differs)

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

ArrayStoreException

A

what we get if we attempt to store an object of type X in an array of type Y
(remember: array lists have a homogenous data structure)

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

interface

A

establishes a set of public methods that any class which implements that interface must contain
ALL METHODS ARE PUBLIC STATIC
ALL VARIABLES ARE PUBLIC STATIC FINAL

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

Comparable

A
> an interface that is defined by the standard Java libraries
> a class that implements comparable interface needs public method: int compareT (T otherObject) 
>has one method : compareTo
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Arrays.

A

just like math. java has a utility class named Arrays that contains a variety of general-purpose methods

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

Arrays.sort()

A

takes any array of an object type as long as that type implements the Comparable interface
> negative number if parameter comes before method
> zero if parameter equals method
> positive number if parameter comes after method

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

how to implement comparable interface

A

public class Rational implements Comparable {

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

@override

A
key word for interfaces
(causes a compilation error if the signature of the class method doesn’t match the signature of the interface method)
(not required, because there are no differences behind the scenes)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

how to implement an interface

A

public class Cat implements Animal {

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

how many interfaces can a class implement

A

as many as you want

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

heterogeneous array / array list!!!

A

if we make an array of animal references, it can have dog and cat objects

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

why interfaces need casting:

A

We can use (type)casting to specify what the object’s actual data type is and then we can dereference it using that to gain access to non-interface methods

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

what if you want an animal interface to also be a comparable interface?

A

a newly-defined interface can extend an existing one:

public interface ComparableAnimal extends Comparable {

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

what if you only wanted some of your animals to also be comparable?

A

public class ComparableCat implements Animal, Comparable {

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

default method

A

allowed to have a default implementation of a method within the interface definition itself

default public String makeSound(){ return “um”; }

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

things interfaces still can’t do…

A

> enforce that constructors are being written at the class level by the implementing class (since the name of the constructor is the same as the name of the class)

> define instance fields in the class

> define private static fields in the class