File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

IS-A/HAS-A relationship

 
Saibabaa Pragada
Ranch Hand
Posts: 162
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
  • 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?
     
    Saibabaa Pragada
    Ranch Hand
    Posts: 162
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Marshal
    Pie
    Posts: 20836
    75
    C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
     
    Prasad Kharkar
    Ranch Hand
    Posts: 446
    1
    Eclipse IDE MySQL Database Tomcat Server
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Marshal
    Pie
    Posts: 20836
    75
    C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 2066
    Clojure IntelliJ IDE Linux
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I think, this question doesn't have the context of checking our real concepts. Leave this question. That's the best idea!
     
    Saibabaa Pragada
    Ranch Hand
    Posts: 162
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Sorry. My mistake. Let me change the question
     
    Prasad Kharkar
    Ranch Hand
    Posts: 446
    1
    Eclipse IDE MySQL Database Tomcat Server
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    well then it is clear now
  • Dog IS-A Animal
  • Dog IS-A Dog

  • hope this helps
     
    Saibabaa Pragada
    Ranch Hand
    Posts: 162
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 29
    Android Eclipse IDE Firefox Browser
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.
     
    Stephan van Hulst
    Bartender
    Pie
    Posts: 4846
    34
    Chrome Netbeans IDE Windows
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.
     
    Saibabaa Pragada
    Ranch Hand
    Posts: 162
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 9497
    22
    Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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...
     
    Saibabaa Pragada
    Ranch Hand
    Posts: 162
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic