Considering the following code:
@override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Employee employee = (Employee) o;
return Double.compare(employee.salary, salary) == 0 && id == employee.id && name.equals(employee.name) && hireDay.equals(employee.hireDay);
}
since java is only "Call by Value", the method call is equivalent to:
Object o = x;
which is legal to assign a subclass var to a superclass type.
Now, as far as I learned, down-casting without using "instanceOf" operator throws "ClassCastException" at Run-Time. Why everything Works fine in this Scenario?
my second question is: what does instanceof operator do exactly and how JVM handles this operator. Why the Run-Time Environment does not know to the class hierarchy and It knows after instanceof operator?
if(o instanceof Employee)
Employee em = (Employee) o;
The "instanceof" is an expression to boolean. What difference it makes if we evaluate the condition to true?
Employee em;
if(true)
em = (Employee) o;
I have tried many forms of Type-Casting and all require to check type at Run-Time.
I appreciate your response in advance.