Created
June 21, 2020 18:46
-
-
Save Audhil/76da0ea6551ce439e160c3e66164ceb2 to your computer and use it in GitHub Desktop.
UpCasting & DownCasting in Java - the F*ck you need to know
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package _0UpDownCastJava; | |
// https://www.geeksforgeeks.org/upcasting-vs-downcasting-in-java/ | |
public class UpDownCastDemo { | |
static class Parent { | |
String name; | |
void someMetho() { | |
System.out.println("yup I'm from Parent! Name: " + name); | |
} | |
} | |
static class Child extends Parent { | |
int id; | |
@Override | |
void someMetho() { | |
// super.someMetho(); | |
System.out.println("yup I'm from Child! Id: " + id); | |
} | |
} | |
public static void main(String[] args) { | |
// 1 | |
// Upcasting | |
Parent p = new Child(); | |
p.name = "yup"; | |
/* | |
* o/p - yup I'm from Child! Id: 0 | |
* only if | |
@Override | |
void someMetho() { | |
System.out.println("yup I'm from Child! Id: " + id); | |
} | |
* */ | |
// and | |
/* | |
* o/p - yup I'm from Parent! Name: yup | |
yup I'm from Child! Id: 0 | |
* only if | |
@Override | |
void someMetho() { | |
super.someMetho(); | |
System.out.println("yup I'm from Child! Id: " + id); | |
} | |
* */ | |
// uncomment this | |
p.someMetho(); | |
p.name = "hihi"; | |
// 2 | |
// Explicitly downcasting | |
Child child = (Child) p; // Explicitly Downcasting | |
child.id = 9; | |
/* | |
* o/p - yup I'm from Child! Id: 9 | |
* only if | |
@Override | |
void someMetho() { | |
System.out.println("yup I'm from Child! Id: " + id); | |
} | |
* */ | |
// and | |
/* | |
* o/p - yup I'm from Parent! Name: hihi | |
yup I'm from Child! Id: 9 | |
* only if | |
@Override | |
void someMetho() { | |
super.someMetho(); | |
System.out.println("yup I'm from Child! Id: " + id); | |
} | |
* */ | |
// uncomment this | |
child.someMetho(); | |
// 3 | |
// implicitly downcasting | |
// Child dd = new Parent(); // implicitly downcasting - compile time error | |
// 4 | |
// my try | |
// ClassCastException | |
// Parent parent = new Parent(); | |
// Child child1 = (Child) parent; | |
// child1.id = 999; | |
// child1.someMetho(); | |
// 5 | |
// my try | |
Child child2 = new Child(); | |
Parent parent1 = (Parent) child2; | |
parent1.name = "wtf"; | |
/* | |
* o/p - yup I'm from Child! Id: 0 | |
* only if | |
@Override | |
void someMetho() { | |
System.out.println("yup I'm from Child! Id: " + id); | |
} | |
* */ | |
// and | |
/* | |
* o/p - yup I'm from Parent! Name: wtf | |
yup I'm from Child! Id: 0 | |
* only if | |
@Override | |
void someMetho() { | |
super.someMetho(); | |
System.out.println("yup I'm from Child! Id: " + id); | |
} | |
* */ | |
parent1.someMetho(); | |
// 6 | |
Parent parent2 = new Parent(); | |
/* | |
* o/p - yup I'm from Parent! Name: null | |
* only if | |
@Override | |
void someMetho() { | |
System.out.println("yup I'm from Child! Id: " + id); | |
} | |
* */ | |
parent2.someMetho(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment