Advanced Class Design Flashcards Preview

Java8 > Advanced Class Design > Flashcards

Flashcards in Advanced Class Design Deck (17)
Loading 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();
1. hippo instanceof Hippo //?
2. hippo instanceof Anuimal //?
3. hippo instanceof Elephant //?
4. null instanceof Hippo //?

Hippo hippo = new Hippo();
5. hippo instanceof Elephant?

1. True
2. True
3. True
4. false
5. 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() {
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?



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


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

1. Season.values() -> returns an array of all values
2. -> name of the enums
3. Season.ordinal() -> array index of each values


restrictions in Enum

1. Creating an enum refernce that do not exist.
eg.., Season s1 = Seasons.valueOf("summer"). -> if summer is not in Enums constant

2. Enums cannot extend any other enum

3. Enums constructors are private. It cannot be called from outside


Important point to keep in mind in Enums.

If Enums have more than values (like methods etc), it should have semicolon at the end of Enum constants


4 types of nested Classes?

1. Inner Class
2. Method Inner class
3. Anonymous Inner class
4. Static Inner class