It's not a secret anymore!
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes IS-A/HAS-A relationship Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "IS-A/HAS-A relationship" Watch "IS-A/HAS-A relationship" New topic
Author

IS-A/HAS-A relationship

Saibabaa Pragada
Ranch Hand

Joined: Oct 24, 2010
Posts: 162
Hi, This is from Whizlabs. Question is: Please select 2 correct options from the following

1. Animal IS-A Dog
2. Dog IS-A Animal
3. Dog IS-A Dog
4. Animal HAS-A Dog
5. Dog HAS-A Animal

I am thinking that one of the answers is Dog IS-A Animal and what is the other ?
Prasad Kharkar
Ranch Hand

Joined: Mar 07, 2010
Posts: 446
    
    1

  • Dog does not extend Animal class so Dog IS NOT Animal
  • Dog class does not have reference to Animal and also Animal class does not have reference to Dog so they do not have any relation



  • did you post the correct code?

    SCJP 6 [86%], OCPWCD [84%], OCEJPAD [83%]
    If you find any post useful, click the "plus one" sign on the right
    Saibabaa Pragada
    Ranch Hand

    Joined: Oct 24, 2010
    Posts: 162
    Yes prasad. I posted the right code. Second answer is, Dog IS-A Dog..I don't know how come this is possible.
    Henry Wong
    author
    Sheriff

    Joined: Sep 28, 2004
    Posts: 20029
        
      44

    Saibabaa Pragada wrote:
    I am thinking that one of the answers is Dog IS-A Animal and what is the other ?


    From the code that you presented, this is *not* true. A Dog instance is not an Animal instance in your example.

    Henry


    Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
    Prasad Kharkar
    Ranch Hand

    Joined: Mar 07, 2010
    Posts: 446
        
        1

    well maybe Dog IS-A Dog can be true as a dog instance will always be of Dog type
    what is the second answer provided?
    please provide both answers
    Henry Wong
    author
    Sheriff

    Joined: Sep 28, 2004
    Posts: 20029
        
      44

    Saibabaa Pragada wrote:Second answer is, Dog IS-A Dog..I don't know how come this is possible.


    Are you saying that a Dog instance is not a Dog instance?

    As a side note, the java specifications actually mentions that any class type is its own type -- identity is supported. It's mainly done to make the specification clearer. And also to allow redundant casts in expressions.

    Henry
    Abimaran Kugathasan
    Ranch Hand

    Joined: Nov 04, 2009
    Posts: 2066

    I think, this question doesn't have the context of checking our real concepts. Leave this question. That's the best idea!


    |BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
    Saibabaa Pragada
    Ranch Hand

    Joined: Oct 24, 2010
    Posts: 162
    Sorry. My mistake. Let me change the question
    Prasad Kharkar
    Ranch Hand

    Joined: Mar 07, 2010
    Posts: 446
        
        1

    well then it is clear now
  • Dog IS-A Animal
  • Dog IS-A Dog

  • hope this helps
    Saibabaa Pragada
    Ranch Hand

    Joined: Oct 24, 2010
    Posts: 162
    How come Dog IS-A Dog ?
    I was told that IS-A means, we should use either extends or implements Runnable. Could you please let me know why/how we can say Dog IS-A Dog
    AmanZeeK Verma
    Greenhorn

    Joined: Dec 06, 2010
    Posts: 29

    I was told that IS-A means, we should use either extends or implements Runnable.
    --> Please cross check

    This is seriously going off the track!..

    Consider following points:
    1) Since its an objective question, which has various options and Dog IS-A Animal is fine. Only other option that can be selected is Dog IS-A Dog, all other options are 100% wrong.

    2) However please note a code like

    will not compile, saying Cyclic Dependency.

    3) benefit of doubt to Dog IS-A Dog since you can cast a Dog instance to Dog Type.

    Aman ZeeK Verma
    Stephan van Hulst
    Bartender

    Joined: Sep 20, 2010
    Posts: 3983
        
      18

    Yup. Here's a simply trick to see if the IS-A relationship holds:

    B b;
    A a = b;

    Replace A and B with any class. If the compiler doesn't complain, then B is an A.


    The mind is a strange and wonderful thing. I'm not sure that it will ever be able to figure itself out, everything else, maybe. From the atom to the universe, everything, except itself.
    Saibabaa Pragada
    Ranch Hand

    Joined: Oct 24, 2010
    Posts: 162
    Rectified typo, I was told that IS-A means, we should use either extends or implements Runnable should be read as "I was told that IS-A means, we should use either extends or implements"
    Ankit Garg
    Sheriff

    Joined: Aug 03, 2008
    Posts: 9400
        
      20

    Saibabaa Pragada wrote:I was told that IS-A means, we should use either extends or implements

    And you were told that by who?? Read what Henry wrote...


    SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
    Saibabaa Pragada
    Ranch Hand

    Joined: Oct 24, 2010
    Posts: 162
    I was told in the class and It is clear now. One class type(example:- Dog) can be type casted with the same type(i.e., Dog) and doesn't give compile/runtime errors. Thanks everybody
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: IS-A/HAS-A relationship
     
    It's not a secret anymore!