• 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

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
1
  • 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).
 
It is no measure of health to be well adjusted to a profoundly sick society. -Krishnamurti Tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic