From 0b1fb6c74a80cfb33180e6570692eaafa98305f3 Mon Sep 17 00:00:00 2001 From: ivs Date: Wed, 23 Oct 2024 18:30:11 +0800 Subject: [PATCH] Extra comments --- T5Q2-C/src/Main.java | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/T5Q2-C/src/Main.java b/T5Q2-C/src/Main.java index ad8eec9..a1ebe2d 100644 --- a/T5Q2-C/src/Main.java +++ b/T5Q2-C/src/Main.java @@ -3,7 +3,6 @@ public class Main { // I - Doesn't work because downcast needs to be explicitly declared // ClassC c = new ClassD(); // ClassE e = c; -// ClassE e = (ClassE) c; // II - CanĀ“t upcast because they are from different family branches // ClassB b = new ClassE(); @@ -14,21 +13,18 @@ public class Main { // a.print(12, "there"); // Ok // 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 - // ObjectType <- ReferenceType - // Compiler always keep tracking of ObjectType - // While only during Runtime that we will check and execute the ReferenceType + // IV - // ClassA a = new ClassC(); // Ok -> Upcast A<-C -// ClassG g = (ClassG)a; // Ok -> Downcast G<-(A<-C) -// g.print("hello"); // RunTime error: trick - // Upcast is prone to Compiler error +// ClassG g = (ClassG)a; // Compile Error on some JDKs -> Downcast G<-(A<-C) +// g.print("hello"); // RunTime error + // Upcast is prone to Compiler error and 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 // ClassA a = new ClassC(); // ok -// ClassG g = (ClassG)a; // ok -// g.print("hello", "there"); // Runtime error: trick +// ClassG g = (ClassG)a; // Compile Error on some JDKs +// g.print("hello", "there"); // Runtime error // VI // ClassA a = new ClassF(); // OK