Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Generics and collections K&B pg 526

 
sweety singh
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi.. i have a doubt in the following code:


// i did not understand "o instanceof Moof"... how come object IS A Moof... object is a superclass... and there is no Moof object assigned to 'o'..
thanks...
 
Ian Edwards
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Every object inherits the equals() method from the Object class. You can then override it to carry out whichever equality tests are required for the class.

What this means is that you must use the same method signature as the Object class. This isn't a problem as every class extends from the Object class therefore every object IS-A Object. Even though the variable o is declared as being of type Object it is still a Moof object. However, in order to access any Moof members you need to first cast it to a Moof object.

Before you try treating o as a Moof object you must first check that it is indeed a Moof object. If it was for example a Dog object and you try accessing a Moof method you would get a ClassCastException.
 
sweety singh
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic