BAEL-3854 - Pattern Matching for instanceof in Java 14
This commit is contained in:
parent
08506092e8
commit
f0e23cabda
|
@ -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() {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue