interfaces and inner classes Flashcards

1
Q

what is an interface

A
extreme case of an abstract class
not a class
a type that can be satisfied by any class that implements that interface
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

what does an interface do

A

specifies a set of methods that any class that implements the interface must have

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

what does an abstract class have

A
  • contains method headings and constant definitions only
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

how does java approximate multiple inheritance

A

through interfaces

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

how do methods with interface parameter types work

A

that parameter will accept as an argument any class that implements the interface

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

how must a concrete class implement an interface

A

implements InterfaceName

must implement all the method headings listed in the definitions of the interfaces

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

abstract class implementing interfaces

A

may implement one or more interfaces
Any method headings given in the interface that are not given definitions are made into abstract methods

A concrete class must give definitions for all the method headings given in the abstract class and the interface

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

derived interfaces

A

extends BaseInterface

A concrete class that implements a derived interface must have definitions for any methods in the derived interface as well as any methods in the base interface

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

interface pitfall

A

runtime and compiler systems dont check that the interface body is consistent with the intended meaning

If the method body does not satisfy the specified semantics, then software written for classes that implement the interface may not work correctly

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

what rules must total ordering satisfy?

A

irreflexivity - for no object o does o come before o
Trichotomy - for any objects o1 and o2 only of of the following are true: o1 comes before o2, o1 comes after o2, o1 == o2
transivity - if o1 comes before o2 and o2 comes before o3 the o1 comes before o3

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

what inconsistencies happen when a class implements two interfaces

A

One type of inconsistency will occur if the interfaces have constants with the same name, but with different values
Another type of inconsistency will occur if the interfaces contain methods with the same name but different return types

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

when will the class definition using more than 1 interface be illegal?

A

implements two inconsistent interfaces

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

what is the serializable interface?

A
  • no method headings or constants - completely empty

- used merely as a type tag that indicates to the system that it may implement file I/O in a particular way

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

what is the cloneable interface

A

no method heading or contained constants

used to indicate how the method clone (inherited from the Object class) should be used or redefined

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

when implementing Cloneable for primitive or immutable classes

A

just use the existing Object.clone

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

when implementing Cloneable for mutable classes

A
First invoke the clone method of the base class Object (or whatever the base class is)
Then reset the values of any new instance variables whose types are mutable class types
 This is done by making copies of the instance variables by invoking their clone methods
17
Q

what are inner classes

A
classes that are defined within other classes
An inner class definition is a member of the outer class in the same way that the instance variables and methods of the outer class are members
-An inner class is local to the outer class definition
-The name of an inner class may be reused for something else outside the outer class definition
-If the inner class is private, then the inner class cannot be accessed by name outside the definition of the outer class
18
Q

advantages of inner classes

A
make outer classes more self contained since that are defined inside a class
both of their methods have access to each other's private methods and instance variables
- inner class as a helping class - if helping class the inner class should be made private
19
Q

can inner and outer classes access each other’s private members?

A

in definition of the inner class method definitions:

legal to reference private instance variables of the outer class
legal to invoke private method of the outer class

in definition of the outer class method definitions:

legal to reference a private instance variable of the inner class on the an object of the inner class
- legal to invoke a non static method of the inner class as long as an object of the inner class is used as a calling object
20
Q

true or false: within the definition of the inner or outer classes, the modifiers private and public are equivalent

A

true

21
Q

naming files with multiple classes

A
Compiling a class with one (or more) inner classes causes both (or more) classes to be compiled, and produces two (or more) .class files
Such as ClassName.class and ClassName$InnerClassName.class
22
Q

what is it that allows an inner class definition to reference an instance variable or invoke a method of the outer class

A

A normal inner class has a connection between its objects and the outer class object that created the inner class object

23
Q

in which situations must an inner class be static?

A
  • if an object of the inner class is created withing a static method of the outer class
  • if the inner class must have static members

Since a static inner class has no connection to an object of the outer class, within an inner class method

  • Instance variables of the outer class cannot be referenced
  • Nonstatic methods of the outer class cannot be invoked

To invoke a static method or to name a static variable of a static inner class within the outer class, preface each with the name of the inner class and a dot

24
Q

what if an inner class is named public

A

it can be used outside of the outer class as well

25
Q

what must be done in the case of a non static inner class

A

it must be created using an object of the outer class

BankAccount account = new BankAccount();
BankAccount.Money amount = account.new Money("41.99");

The new object amount can now invoke methods from the inner class, but only from the inner class

26
Q

what must be done in the case of a static inner class

A

OuterClass.InnerClass innerObject = new OuterClass.InnerClass();

Note that all of the following are acceptable
innerObject.nonstaticMethod();
innerObject.staticMethod();
OuterClass.InnerClass.staticMethod();

27
Q

nesting classes

A
Given class A, which has public inner class B, which has public inner class C, then the following is valid:
A aObject = new A();
A.B bObject = aObject.new B();
A.B.C cObject = bObject.new C();
28
Q

what happens with inner classes and inheritance

A
Given an OuterClass that has an InnerClass
-Any DerivedClass of OuterClass will automatically have InnerClass as an inner class
-In this case, the DerivedClass cannot override the InnerClass
An outer class can be a derived class
An inner class can be a derived class also
29
Q

what is an anonymous class

A
If an object is to be created, but there is no need to name the object's class, then an anonymous class definition can be used
-The class definition is embedded inside the expression with the new operator
30
Q

when are anonymous classes used

A
when they are to be assigned to a variable of another type
The other type must be such that an object of the anonymous class is also an object of the other type
The other type is usually a Java interface