Created
October 9, 2021 13:28
-
-
Save nowshad-hasan/2d3115099a55a92f4745138b5eaecec4 to your computer and use it in GitHub Desktop.
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
static void printBookOld(Book book) { | |
if (book instanceof Thriller) { | |
Thriller thriller = (Thriller) book; // must-do explicit casting | |
System.out.println(thriller.getSuspense()); | |
} else if (book instanceof AutoBiography) { | |
AutoBiography biography = (AutoBiography) book; // must-do explicit casting | |
System.out.println(biography.getContribution()); | |
} | |
} | |
static void printBookNew(Book book) { | |
if (book instanceof Thriller thriller) { /* casting in one-line */ | |
System.out.println(thriller.getSuspense()); | |
} else if (book instanceof AutoBiography biography) { /* casting in one-line */ | |
System.out.println(biography.getContribution()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment