Advanced Class Design Flashcards
a instanceof B
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).
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(); } }
”???”
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.
@Override
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.
toString()
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
Rules for the equals() method
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.
Rules for the hashCode() method
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.
enum
A class that represents a fixed set of constants
How to declare an enum
public enum testEnum {
FIRST, SECOND, THIRD
}
Enum method values()
Returns an array of all of the enum values
Example:
public enum testEnum {
FIRST, SECOND, THIRD
}
testEnum.values() // [FIRST, SECOND, THIRD]
Enum method name()
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
}
Enum method ordinal()
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
}
Can you extend an enum?
Noooo
Nested Class
A class that is defined within another class
Inner Class
A nested class that is not static
Member Inner Class
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