Advanced Class Design Flashcards
How to check whether a reference is of type instance?
instanceof operator
class Animal{} class Hippo extends Animal{} class Elephant extends Animal{}
Animal hippo = new Hippo();
- hippo instanceof Hippo //?
- hippo instanceof Anuimal //?
- hippo instanceof Elephant //?
- null instanceof Hippo //?
Hippo hippo = new Hippo(); 5. hippo instanceof Elephant?
- True
- True
- True
- false
- Compiler error
what is the difference between instanceof check on class and interface?
compilation check on instanceof check happens only on class level. When checking instanceof at interface level, then the check happens at runtime.
Why? because there could be a subclass which can implement the interface, which can be known only at runtime
What is virtual method invocation and what are the members they are applicable for?
Calling overridden methods at runtime.
Please note that, they are applicable only to methods and not for instance variables.
abstract class Animal { String name = "???"; public void printName() { System.out.println(name); } } class Lion extends Animal { String name = "Leo"; }
Animal animal = new Lion(); animal.printName() //? output?
???
what is reflection?
technique in java to look at the class information at runtime
If we want to check the equivalence of two objects, what method should we override?
equals()
signature for equals() method?
public boolean equals(Object obj)
contract for Equals method?
It is reflexive: For any non‐null reference value x, x.equals(x) should return true.
■■ It is symmetric: For any non‐null reference values x and y, x.equals(y) should return
true if and only if y.equals(x) returns true.
■■ It is transitive: For any non‐null reference values x, y, and z, if x.equals(y) returns
true and y.equals(z) returns true, then x.equals(z) should return true.
■■ 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.
■■ For any non‐null reference value x, x.equals(null) should return false.
WOW! eBook
www.wowebook.
what is hash code?
It is a number that puts the instances of a class in finite categories
contract on hashcode/
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. In our hippo
example, including the name is fine. Including the weight is not because hippos change
weight regularly.
■■ 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. You saw this in the card example. We used only one
of the variables to determine the hash code.
■■ If equals() returns false when called with two objects, calling hashCode() on each of
those objects does not have to return a different result. This means hashCode() results
do not need to be unique when called on unequal objects.
what is Enumerations?
In Java, it is a fixed set of constants.It is a class in java
How does it different from other constants?
It provides type check
what are the three basic methods in Enum? Eg.. if Season is a Enum
- Season.values() -> returns an array of all values
- Season.name() -> name of the enums
- Season.ordinal() -> array index of each values
restrictions in Enum
- Creating an enum refernce that do not exist.
eg. ., Season s1 = Seasons.valueOf(“summer”). -> if summer is not in Enums constant - Enums cannot extend any other enum
- Enums constructors are private. It cannot be called from outside