diff --git a/core-java/src/main/java/com/baeldung/casting/Animal.java b/core-java/src/main/java/com/baeldung/casting/Animal.java new file mode 100644 index 0000000000..9f31c1dda3 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/casting/Animal.java @@ -0,0 +1,13 @@ +package com.baeldung.casting; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class Animal { + private static final Logger LOGGER = LoggerFactory.getLogger(Animal.class); + + public void eat() { + LOGGER.info("animal is eating"); + } + +} diff --git a/core-java/src/main/java/com/baeldung/casting/AnimalFeeder.java b/core-java/src/main/java/com/baeldung/casting/AnimalFeeder.java new file mode 100644 index 0000000000..89b972e5c2 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/casting/AnimalFeeder.java @@ -0,0 +1,23 @@ +package com.baeldung.casting; + +import java.util.List; + +public class AnimalFeeder { + + public void feed(List animals) { + animals.forEach(animal -> { + animal.eat(); + if (animal instanceof Cat) { + ((Cat) animal).meow(); + } + }); + } + + public void uncheckedFeed(List animals) { + animals.forEach(animal -> { + animal.eat(); + ((Cat) animal).meow(); + }); + } + +} diff --git a/core-java/src/main/java/com/baeldung/casting/Cat.java b/core-java/src/main/java/com/baeldung/casting/Cat.java new file mode 100644 index 0000000000..aa9a9a881a --- /dev/null +++ b/core-java/src/main/java/com/baeldung/casting/Cat.java @@ -0,0 +1,16 @@ +package com.baeldung.casting; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class Cat extends Animal implements Mew { + private static final Logger LOGGER = LoggerFactory.getLogger(Cat.class); + + public void eat() { + LOGGER.info("cat is eating"); + } + + public void meow() { + LOGGER.info("meow"); + } +} diff --git a/core-java/src/main/java/com/baeldung/casting/Dog.java b/core-java/src/main/java/com/baeldung/casting/Dog.java new file mode 100644 index 0000000000..763c6b4785 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/casting/Dog.java @@ -0,0 +1,12 @@ +package com.baeldung.casting; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class Dog extends Animal { + private static final Logger LOGGER = LoggerFactory.getLogger(Dog.class); + + public void eat() { + LOGGER.info("dog is eating"); + } +} diff --git a/core-java/src/main/java/com/baeldung/casting/Mew.java b/core-java/src/main/java/com/baeldung/casting/Mew.java new file mode 100644 index 0000000000..f3c7324551 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/casting/Mew.java @@ -0,0 +1,5 @@ +package com.baeldung.casting; + +public interface Mew { + public void meow(); +} diff --git a/core-java/src/test/java/com/baeldung/casting/CastingTest.java b/core-java/src/test/java/com/baeldung/casting/CastingTest.java new file mode 100644 index 0000000000..0ca1ce88dc --- /dev/null +++ b/core-java/src/test/java/com/baeldung/casting/CastingTest.java @@ -0,0 +1,58 @@ +package com.baeldung.casting; + +import org.junit.Test; +import static org.junit.Assert.*; +import java.util.ArrayList; +import java.util.List; + +public class CastingTest { + + @Test + public void whenPrimitiveConverted_thenValueChanged() { + double myDouble = 1.1; + int myInt = (int) myDouble; + assertNotEquals(myDouble, myInt); + } + + @Test + public void whenUpcast_thenInstanceUnchanged() { + Cat cat = new Cat(); + Animal animal = cat; + animal = (Animal) cat; + assertTrue(animal instanceof Cat); + } + + @Test + public void whenUpcastToObject_thenInstanceUnchanged() { + Object object = new Animal(); + assertTrue(object instanceof Animal); + } + + @Test + public void whenUpcastToInterface_thenInstanceUnchanged() { + Mew mew = new Cat(); + assertTrue(mew instanceof Cat); + } + + @Test + public void whenUpcastToAnimal_thenOverridenMethodsCalled() { + List animals = new ArrayList<>(); + animals.add(new Cat()); + animals.add(new Dog()); + new AnimalFeeder().feed(animals); + } + + @Test + public void whenDowncastToCat_thenMeowIsCalled() { + Animal animal = new Cat(); + ((Cat) animal).meow(); + } + + @Test(expected = ClassCastException.class) + public void whenDownCastWithoutCheck_thenExceptionThrown() { + List animals = new ArrayList<>(); + animals.add(new Cat()); + animals.add(new Dog()); + new AnimalFeeder().uncheckedFeed(animals); + } +}