From f0e23cabda49ab7768d36652b30627553dbf0a57 Mon Sep 17 00:00:00 2001 From: Jonathan Cook Date: Sun, 1 Mar 2020 08:36:51 +0100 Subject: [PATCH] BAEL-3854 - Pattern Matching for instanceof in Java 14 --- .../PatternMatchingForInstanceOf.java | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 core-java-modules/core-java-14/src/main/java/com/baeldung/java14/patternmatchingforinstanceof/PatternMatchingForInstanceOf.java diff --git a/core-java-modules/core-java-14/src/main/java/com/baeldung/java14/patternmatchingforinstanceof/PatternMatchingForInstanceOf.java b/core-java-modules/core-java-14/src/main/java/com/baeldung/java14/patternmatchingforinstanceof/PatternMatchingForInstanceOf.java new file mode 100644 index 0000000000..ccade84bea --- /dev/null +++ b/core-java-modules/core-java-14/src/main/java/com/baeldung/java14/patternmatchingforinstanceof/PatternMatchingForInstanceOf.java @@ -0,0 +1,26 @@ +package com.baeldung.java14.patternmatchingforinstanceof; + +public class PatternMatchingForInstanceOf { + + public void performAnimalOperations(Animal animal) { + if (animal instanceof Cat cat) { + cat.meow(); + } else if(animal instanceof Dog dog) { + dog.woof(); + } + } + + private abstract class Animal { + } + + private final class Cat extends Animal { + private void meow() { + } + } + + private final class Dog extends Animal { + private void woof() { + } + } + +}