• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

IS-A relationship

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
im currently studying for my programmers certification and i've come across this question twice, but with opposite answers.

can anyone clear it up just in case it pops up on the real exam.

TRUE or FALSE:

"is-a relationships always require at least two class types"



(i heard that a 'type' can be 'of its own' type i.e Object is-a Object. one class, therefore the is-a relationship statement above is false)
 
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Gosh, what a silly exam question! They're not all like that, are they?

This looks like an example of a degenerate case. Normally, an "is a" relationship is between two different classes. But the degenerate case is where the two classes are the same: "Dog is a Dog", for example. Such a degenerate case is handled correctly by Java, as in "instanceof" operator.

Dog instanceof Animal --> true
Dog instanceof Dog --> true
Dog instanceof Cat --> false
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Peter Chase:
Gosh, what a silly exam question! They're not all like that, are they? ...


Thankfully, no. I wouldn't expect the real exam to be this poorly worded. This example has come up repeatedly in the SCJP forum (where I'm moving this post).

In addition to the single-class issue (Dog IS-A Dog), also consider that an interface is not a class, but it is a type.

So, for example, if Animal is an interface, and Dog is a class that implements Animal, then an instance of Dog IS-AN Animal (and instanceof will return true).
 
We noticed he had no friends. So we gave him this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic