base1 b1 = new constructor("apple"); //1
base1 b2 = new base1("apple"); //2
System.out.println("b1.equals.b2"+b1.equals(b2)); //3
b1=b2; //4
System.out.println("b1.equals.b2"+b1.equals(b2)); //5
During conversion and casting the compile-time data type of variable is considered, not the run-time data type.
when you say, base1 b1 = new constructor("apple");
Compile time data type of b1 is base1 not constructor.
so, at line 4 you can say b1 = b2; (no cating)
But, when you say,
constructor b1 = new constructor("apple");
Now, compile-time data type of b1 is constructor.
then for b1 = b2; compiler shouts and ask for casting.
b1 = (constructor) b2; , This will compile.
B'cos, casting rule is that new type must be super class of old type or VICE-VERSA.
But surely, it will give runtime error, ClassCastException
b'cos b2 (obj of base1) can't assign to b1( of type constructor)
Casting rules are more loose than conversion rule.
Hope this clarifies.
Regards,
Sujit