Chapter 1: Advanced Class Design Flashcards

1
Q

How does Java match overloaded methods

A

When matching overloaded methods, Java checks for:

  • Exact match
  • Match by superclass
  • Converting to a larger primitive
  • Converting to autoboxed
  • Varargs
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

When is instanceof true?

A

a instanceof b. True if A is an instance of B, a subclass of B, or a class which implements the B interface (directly or indirectly)

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

Instanceof causing a compilation error?

A

If the compiler knowns that A can never be B. (Except when B is an interface. Even if your class A is final, you can still check instanceof interface. This is because you could have a subclass which implements the interface and this only gets checked at runtime)

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

Virtual method invocation

A

This is when we invoke regular non-static methods. The reason it virtual method invocation is because Java can look for overriden methods rather than the ones in the class the compiler says we have.

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

@Override

A

For @Override to work correctly, the method must be doing one of these:
* Implementing from an interface
* Overriding a superclass method
* Overriding hashCode, equals, toString()
Otherwise there will be a compilation error

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

Equals

A

Signature is public boolean equals(Object obj). If you use anything other than Object it is an overloaded method (not overriden).

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

Rules for overriding Equals

A
  • x.equals(x) == true
  • x.equals(y) y.equals(x) both true or both false
  • transitive x.eq(y) y.eq(z) then z.eq(x)
  • must be consistent, so you can do it multiple times with the same result
  • x.eq(null) == false. Note: Shouldn’t throw NPE!
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Overriding hashCode

A

A hashCode is a number which puts instances of a class into a finite number of categories. (Makes it easier to sort in a HashMap).

  • Common not to include Boolean or ‘char’ values
  • Within the same program, the result of a hashCode must not change. (i.e. don’t include variables which are likely to change)
  • Can use a subset of variables which equals() uses
  • hashCode() results don’t need to be unique when called on unequal results
  • Common to multiply by a prime number to make the hashCode more unique
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Enum values

A

.values() returns an array of all the values

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

Enum ordinal

A

.ordinal() is the int value that corresponds to the order the enum was declared. But you can’t compare enum to int, so watch out for this.

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

Enum valueOf

A

.valueOf(“”) returns the enum value of the exact match. Note it is case-sensitive. If it can’t find the enum value it will throw an IllegalArgumentException.

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

Enums in a switch statement

A

Needs to be just the ENUM value, can’t compare to int ordinal, or String values

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

Semi-colon in enums

A

Semi-colon is needed at the end of the enums if there is anything other than just the enum values

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

Enum constructors

A

The constructor is called once when you first call any Enum value.

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

Adding abstract methods to enums

A

Can add an abstract method into an enum, but then all enums must override this otherwise there will be a compilation error.

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

Member inner classes

A
  • Defined at the same level as methods, instance vars and constructors
  • No restrictions on accessibility (can be public or private)
  • Can be abstract or final
  • Cannot declare static fields or methods
  • Can access private members of outer class
  • You need an instance of the outer class to be able to instantiate the inner class
17
Q

Same variable names in inner class

A

A > B > C nesting with all having a variable x
If you’re calling from deepest level C:
* this.x will be C.x
* B.this.x will be B.x
* A.this.x will be A.x.
If you’re trying to get the x from C, but you’re in in B, this won’t work

18
Q

Instantiating member inner classes

A
A a = new A();
A.B b = a.new B(); (Note: could have used the reference B here since B is a member of A)
A.B.C c = b.new C(); (Note: Here we have to reference the full path to C since C is not a member of A).
19
Q

Inner interfaces

A

Can be private

e.g. Can only be referred to from outer class

20
Q

Local inner classes

A
  • Nested class defined within a method.
  • Can only be accessed within that method (and doesn’t have an access modifier)
  • Cannot be declared static, or have any static fields or methods
  • Does not exist until a method is invoked
  • Has access to all fields and methods of the enclosing class.
  • Does not have access to local variables of the method, unless they are final or effectively final (Since it will exist in a separate file. But if variables are final or effectively final they will be passed in the constructor to the class when it is instantiated)
21
Q

Effectively final

A
  • We don’t change the reference to the object, even if a change occurs in the state of the referenced object
  • It doesn’t have to be initialised in the same line it is declared.
  • Just make sure it doesn’t change reference after it’s been initialised.
22
Q

Anonymous inner classes

A
  • Extend an existing class or implement an interface
  • Short implementation not used anywhere else
  • You can just override the methods you need directly
  • Note that interface implementations must be public
  • Can define them in a parameter of another method
23
Q

Static nested classes

A
  • Defined at member level
  • Don’t need an instance of out class to use
  • Can import statically or non-statically
  • Enclosing class can refer to methods + fields of static class