Skip to content
Advertisement

How can I use my Functional interface to return me a Cat instead of a Kitten?

So, I have 2 classes: Cat and Kitten extends Cat. They have fields (int age, String name) and a constructor. I am trying to create my Functional interface that takes Kitten as input parameter and returns me a Cat (and makes it 2 years elder).

JavaScript

And Main class

JavaScript

I don’t have an idea where I can change its age and I have ClassCastException as well. What am I do wrong?

Advertisement

Answer

  • You can try implementing this by using your functional interface :

  • Below Lambda expression will take Kitten as input and make it’s age 2 year elder

    JavaScript
  • Call the grow function by passing kitten object and it will returns the Cat incremented age with 2 year elder.

    JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement