advanced patterns with Abstract Classes and Interfaces in Java 17
I'm relatively new to this, so bear with me. I've searched everywhere and can't find a clear answer. I'm working with an scenario while trying to implement an abstract class that uses an interface in Java 17. I have an abstract class `Animal` that declares a method `makeSound()`, and an interface `Mammal` that has a method `giveBirth()`. The question arises when I try to extend `Animal` in a concrete class `Dog`, which also implements `Mammal`. Hereโs the code: ```java abstract class Animal { public abstract void makeSound(); } interface Mammal { void giveBirth(); } class Dog extends Animal implements Mammal { @Override public void makeSound() { System.out.println("Woof!"); } @Override public void giveBirth() { System.out.println("Giving birth to puppies!"); } } ``` When I try to create an instance of `Dog` and call its methods, I'm getting a `NullPointerException` when invoking `giveBirth()`. Hereโs the instantiation: ```java public class Main { public static void main(String[] args) { Dog dog = new Dog(); dog.makeSound(); dog.giveBirth(); } } ``` The output for `makeSound()` is correct, but when I call `giveBirth()`, I get this behavior: ``` Exception in thread "main" java.lang.NullPointerException at Dog.giveBirth(Dog.java:10) ``` Iโve checked the implementation of `giveBirth()`, and it seems straightforward. I suspect it might be related to how Java handles interfaces and method calls. I also considered whether I missed overriding a method or if there's an scenario with Java's object lifecycle, but I need to find any issues. Any ideas on why this `NullPointerException` might be occurring? I've also tried running this in a separate IDE just to verify if itโs an environment question, but I still get the same behavior. I'm working on a CLI tool that needs to handle this. I'd really appreciate any guidance on this. For context: I'm using Java on macOS.