Advanced Class Design Flashcards

1
Q

a instanceof B

A

Returns true if the reference to which a points is an instance of class B, a subclass of B (directly or indirectly), or a class that implements the B interface (directly or indirectly).

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

What is the output?

abstract class Animal { 
     String name = "???"; 
     public void printName() {
         System.out.println(name); 
     }
}
class Lion extends Animal {
      String name = "Leo"; 
}
public class PlayWithAnimal {
       public static void main(String... args) {
           Animal animal = new Lion();
           animal.printName(); }
}
A

”???”

This is because name is an instance variable, so it uses the version that is in the class it is used in.

When it comes to methods, an overridden method will be used even from a superclass.

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

@Override

A

Can be put before a method to indicate that it will be overriding a method from a superclass or interface. If the method that follows does not in fact override another method then you’ll get a compiler error.

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

toString()

A
A method in the Object class that many classes override in order to print out the details of an object in a readable format. 
When you do System.out.println(obj) it will automatically try to call the toString() method on obj
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Rules for the equals() method

A

1) It is reflexive: x.equals(x) should return true.

2) It is symmetric: x.equals(y) should return
true if and only if y.equals(x) returns true.

3) It is transitive: if x.equals(y) returns
true and y.equals(z) returns true, then x.equals(z) should return true.

4) For any non‐null reference value x, x.equals(null) should return false. NOT NullPointerException.
5) It is consistent: For any non‐null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.

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

Rules for the hashCode() method

A

1) Within the same program, the result of hashCode() must not change. This means that you shouldn’t include variables that change in figuring out the hash code.
2) If equals() returns true when called with two objects, calling hashCode() on each of those objects must return the same result. This means hashCode() can use a subset of the variables that equals() uses.
3) If equals() returns false when called with two objects, calling hashCode() on each of those objects does NOT have to return a different result.

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

enum

A

A class that represents a fixed set of constants

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

How to declare an enum

A

public enum testEnum {
FIRST, SECOND, THIRD
}

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

Enum method values()

A

Returns an array of all of the enum values

Example:

public enum testEnum {
FIRST, SECOND, THIRD
}

testEnum.values() // [FIRST, SECOND, THIRD]

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

Enum method name()

A

Returns the name of the enum constant

public enum testEnum {
FIRST, SECOND, THIRD
}

for (testEnum e: testEnum.values()) {
System.out.print(e.name()); //FIRST, then SECOND, the THIRD
}

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

Enum method ordinal()

A

Returns the ordinal value of the enum constant

public enum testEnum {
FIRST, SECOND, THIRD
}

for (testEnum e: testEnum.values()) {
System.out.print(e.ordinal()); //1, then 2, then 3
}

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

Can you extend an enum?

A

Noooo

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

Nested Class

A

A class that is defined within another class

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

Inner Class

A

A nested class that is not static

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

Member Inner Class

A

Defined at the member level of a class (the same level as the methods, instance variables, and constructors).

  • Can be declared public, private, or protected or use default access
  • Can extend any class and implement interfaces
  • Can be abstract or final
  • Cannot declare static fields or methods
  • Can access members of the outer class including private members
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

If a class called Outer contains an inner class called Inner, what are the two class files generated?

A

Outer.class

Outer$Inner.class

17
Q

Local Inner Class

A

A nested class defined in a method

  • They do not have an access specifier.
  • They cannot be declared static and cannot declare static fields or methods.
  • They have access to all fields and methods of the enclosing class.
  • They do not have access to local variables of a method unless those variables are final or effectively final. If the code could still compile with the keyword final inserted before the local variable, the variable is effectively final.
18
Q

Anonymous Inner Class

A

A local inner class that does not have a name. The block with “SaleTodayOnly sale = …” is considered the anonymous inner class

public class AnonInner {
     abstract class SaleTodayOnly {
           abstract int dollarsOff(); 
     }
    public int admission(int basePrice) { 
           SaleTodayOnly sale = new SaleTodayOnly() {
                 int dollarsOff() { return 3; } 
           };
           return basePrice - sale.dollarsOff();
     }
}
19
Q

Static Nested Class

A

A static nested class is a static class defined at the member level. Since it is static, it can be instantiated without an object of the enclosing class, so it can’t access the instance variables without an explicit object of the enclosing class.

20
Q

How to import a static nested class?

A

Either with a regular import or a static import. Either will work.

21
Q

Method Signature of .equals() from Object

A

public boolean equals (Object obj)

22
Q

What is the rule involving a semicolon when declaring an enum?

A

The semicolon at the end of the enum values is optional only if the only thing in the enum is that list of values.

public enum Season {
WINTER(“Low”), SPRING(“Medium”), SUMMER(“High”), FALL(“Medium”);
private String expectedVisitors;
private Season(String expectedVisitors) {
this.expectedVisitors = expectedVisitors;
}
}

VS

public enum Season {
WINTER, SPRING, SUMMER, FALL
}

23
Q

When does the enum constructor run?

A

The first time that we ask for any of the enum values, Java constructs all of the enum values. After that, Java just returns the already‐constructed enum values.

This means the constructor will only run once no matter how many instances of the enum you create.

24
Q

What must you do if adding an abstract method to an enum?

A

Implement the method for each of the enum values

public enum Season {
WINTER {
public void printHours() {
System.out.println(“9am-3pm”);
}
}, SPRING {
public void printHours() {
System.out.println(“9am-5pm”);
}
}, SUMMER {
public void printHours() {
System.out.println(“9am-7pm”);
}
}, FALL {
public void printHours() {
System.out.println(“9am-5pm”);
}
};

public abstract void printHours();
}

25
Q

What level of access may an enum constructor have?

A

default or private

26
Q

Virtual Method Invocation

A

The subclass method gets called at runtime rather than the type in the variable reference

class Animal {
    public void eat() {
        System.out.println("kibble");
    }
class Lion extends Animal {
    public void eat() {
        System.out.println("steak");
    }
}
  Animal a = new Lion();
  a.eat();   
}

//Prints “steak”

27
Q

What is printed here?

public class FourLegged {
    String walk = "walk,";
    static class BabyRhino extends FourLegged{
        String walk = "toddle,"; 
    }
    public static void main(String[] args) {    
          FourLegged f = new BabyRhino(); 
          BabyRhino b = new BabyRhino(); 
          System.out.println(f.walk); 
          System.out.println(b.walk);
    }
}
A

“walk, toddle”

When it comes to instance variables, it’s the reference type that matters