Skip to content

Instantly share code, notes, and snippets.

@nowshad-hasan
Created October 9, 2021 13:28
Show Gist options
  • Save nowshad-hasan/2d3115099a55a92f4745138b5eaecec4 to your computer and use it in GitHub Desktop.
Save nowshad-hasan/2d3115099a55a92f4745138b5eaecec4 to your computer and use it in GitHub Desktop.
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