Extra comments
This commit is contained in:
parent
f5d5e87fa4
commit
0b1fb6c74a
|
@ -3,7 +3,6 @@ public class Main {
|
||||||
// I - Doesn't work because downcast needs to be explicitly declared
|
// I - Doesn't work because downcast needs to be explicitly declared
|
||||||
// ClassC c = new ClassD();
|
// ClassC c = new ClassD();
|
||||||
// ClassE e = c;
|
// ClassE e = c;
|
||||||
// ClassE e = (ClassE) c;
|
|
||||||
|
|
||||||
// II - Can´t upcast because they are from different family branches
|
// II - Can´t upcast because they are from different family branches
|
||||||
// ClassB b = new ClassE();
|
// ClassB b = new ClassE();
|
||||||
|
@ -14,21 +13,18 @@ public class Main {
|
||||||
// a.print(12, "there"); // Ok
|
// a.print(12, "there"); // Ok
|
||||||
// a.print(88); // Compilation error
|
// a.print(88); // Compilation error
|
||||||
|
|
||||||
// IV - We do not have access to a print(String) from the ref C, even though it was downcasted to the obj G
|
// IV -
|
||||||
// ObjectType <- ReferenceType
|
|
||||||
// Compiler always keep tracking of ObjectType
|
|
||||||
// While only during Runtime that we will check and execute the ReferenceType
|
|
||||||
// ClassA a = new ClassC(); // Ok -> Upcast A<-C
|
// ClassA a = new ClassC(); // Ok -> Upcast A<-C
|
||||||
// ClassG g = (ClassG)a; // Ok -> Downcast G<-(A<-C)
|
// ClassG g = (ClassG)a; // Compile Error on some JDKs -> Downcast G<-(A<-C)
|
||||||
// g.print("hello"); // RunTime error: trick
|
// g.print("hello"); // RunTime error
|
||||||
// Upcast is prone to Compiler error
|
// Upcast is prone to Compiler error and Runtime error
|
||||||
// Downcast is prone to Runtime error
|
// Downcast is prone to Runtime error
|
||||||
|
|
||||||
|
|
||||||
// V - We lost track of the print(string, string) in C become it become a A first
|
// V - We lost track of the print(string, string) in C become it become a A first
|
||||||
// ClassA a = new ClassC(); // ok
|
// ClassA a = new ClassC(); // ok
|
||||||
// ClassG g = (ClassG)a; // ok
|
// ClassG g = (ClassG)a; // Compile Error on some JDKs
|
||||||
// g.print("hello", "there"); // Runtime error: trick
|
// g.print("hello", "there"); // Runtime error
|
||||||
|
|
||||||
// VI
|
// VI
|
||||||
// ClassA a = new ClassF(); // OK
|
// ClassA a = new ClassF(); // OK
|
||||||
|
|
Loading…
Reference in New Issue