Home > Back-end >  How does compiler understand the key and value part of an object during casting?
How does compiler understand the key and value part of an object during casting?

Time:11-09

I was going through enter image description here

To sum up, i.next() is returning a Map.Entry but we are storing it in an Object Class reference variable because java allows super class reference variable to hold the sub class object and Object class is super class to all.

Why typecasting? because we can't invoke sub class methods on a super class variable.

If you want the iterator to return Map.Entry without type casting, declare Iterator as Map.Entry Iterator like this Iterator<Map.Entry> i. Happy learning!

  • Related