• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Scott Selikoff
Bartenders:
  • Piet Souris
  • Jj Roberts
  • fred rosenberger

Has-A relation

 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi everyone, i have a small doubt. Given the following code:


can we say that class TestIt has-a Animal and class TestIt has-a Dog?

thanks
 
Ranch Hand
Posts: 72
Netbeans IDE Tomcat Server Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ash Gill wrote:hi everyone, i have a small doubt. Given the following code:


can we say that class TestIt has-a Animal and class TestIt has-a Dog?

thanks



No. You cannot say neither. Look closely at the code.

Animal a = new Dog() part is declared and initialized in the method "main()". This is a local reference.
HAS-A is correct when you are dealing with instance reference. Look at this code:



Let me know if you have more questions.
 
Ranch Hand
Posts: 144
Netbeans IDE Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ash Gill wrote:hi everyone, i have a small doubt. Given the following code:


can we say that class TestIt has-a Animal and class TestIt has-a Dog?

thanks



Hi Ash,
i think we could say that if it was instance variable , since TestIt has a reference to class Animal so TestIt has-a Animal and since Dog is-a Animal so TestIt has-a Dog as well....please correct me if you think i'm wrong!
 
Boris Mechkov
Ranch Hand
Posts: 72
Netbeans IDE Tomcat Server Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hama Kamal wrote:

Ash Gill wrote:hi everyone, i have a small doubt. Given the following code:


can we say that class TestIt has-a Animal and class TestIt has-a Dog?

thanks



Hi Ash,
i thinak we can say that , since TestIt has a reference to class Animal so TestIt has-a Animal and since Dog is-a Animal so TestIt has-a Dog as well....please correct me if you think i'm wrong!



Hama,
you cannot say that TestIt HAS-A animal, nor a Dog. The reference is declared in the main() method, so HAS-A is not valid here. HAS-A is related to instance variables only (variables declared outside a method declarations, but within the class declaration)
 
Ash Gill
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Boris, thanks for the explanation. one more doubt, Does the Has-a relation work in case of static references, like in line ##1 below?

 
Hama Kamal
Ranch Hand
Posts: 144
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Boris Mechkov wrote:

Hama Kamal wrote:

Ash Gill wrote:hi everyone, i have a small doubt. Given the following code:


can we say that class TestIt has-a Animal and class TestIt has-a Dog?

thanks



Hi Ash,
i thinak we can say that , since TestIt has a reference to class Animal so TestIt has-a Animal and since Dog is-a Animal so TestIt has-a Dog as well....please correct me if you think i'm wrong!



Hama,
you cannot say that TestIt HAS-A animal, nor a Dog. The reference is declared in the main() method, so HAS-A is not valid here. HAS-A is related to instance variables only (variables declared outside a method declarations, but within the class declaration)


Hi Boris

you are right i've already corrected my statement in the above post.
 
Boris Mechkov
Ranch Hand
Posts: 72
Netbeans IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hama Kamal wrote:

Boris Mechkov wrote:

Hama Kamal wrote:

Ash Gill wrote:hi everyone, i have a small doubt. Given the following code:


can we say that class TestIt has-a Animal and class TestIt has-a Dog?

thanks



Hi Ash,
i thinak we can say that , since TestIt has a reference to class Animal so TestIt has-a Animal and since Dog is-a Animal so TestIt has-a Dog as well....please correct me if you think i'm wrong!



Hama,
you cannot say that TestIt HAS-A animal, nor a Dog. The reference is declared in the main() method, so HAS-A is not valid here. HAS-A is related to instance variables only (variables declared outside a method declarations, but within the class declaration)


Hi Boris

you are right i've already corrected my statement in the above post.



No problem friend. ;-)
 
Boris Mechkov
Ranch Hand
Posts: 72
Netbeans IDE Tomcat Server Java
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ash Gill wrote:Hi Boris, thanks for the explanation. one more doubt, Does the Has-a relation work in case of static references, like in line ##1 below?



Yes, it does work as well.
 
Ash Gill
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hama Kamal wrote:

Ash Gill wrote:hi everyone, i have a small doubt. Given the following code:


can we say that class TestIt has-a Animal and class TestIt has-a Dog?

thanks



Hi Ash,
i think we could say that if it was instance variable , since TestIt has a reference to class Animal so TestIt has-a Animal and since Dog is-a Animal so TestIt has-a Dog as well....please correct me if you think i'm wrong!



hi Hama, thanks. i now understand that if 'a' was an instance variable then TestIt definately Has-A Animal but i m still doubtful about TestIt having a dog.
 
Boris Mechkov
Ranch Hand
Posts: 72
Netbeans IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ash Gill wrote:

Hama Kamal wrote:

Ash Gill wrote:hi everyone, i have a small doubt. Given the following code:


can we say that class TestIt has-a Animal and class TestIt has-a Dog?

thanks



Hi Ash,
i think we could say that if it was instance variable , since TestIt has a reference to class Animal so TestIt has-a Animal and since Dog is-a Animal so TestIt has-a Dog as well....please correct me if you think i'm wrong!



hi Hama, thanks. i now understand that if 'a' was an instance variable then TestIt definately Has-A Animal but i m still doubtful about TestIt having a dog.



The reference variable type determines what a certain class HAS. So class TestIt HAS-A Animal. Remember, object creation happens at runtime. What if we have an overloaded constructor that assigns a CAT to the animal reference (providing a CAT IS-A Animal)? Then would you say the TestIt class has a CAT as well? Bottom line is TestIt HAS-A Animal reference.
 
Hama Kamal
Ranch Hand
Posts: 144
Netbeans IDE Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
so do you guys mean that
TestIt has-a Animal true
TestIt has-a Dog false ?
 
Boris Mechkov
Ranch Hand
Posts: 72
Netbeans IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hama Kamal wrote:so do you guys mean that
TestIt has-a Animal true
TestIt has-a Dog false ?



Yes.
TestIt class has only an Animal. This is determined by the reference variable. If we had this:



...then we can say that Test HAS-A Dog, a Cat and a Car.

 
Hama Kamal
Ranch Hand
Posts: 144
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
but if it was like


we could say that

TestIt has-a Dog and TestIt has-a Animal >>> since we can access all the members of Animal class through object reference variable g .... am i right?
 
Boris Mechkov
Ranch Hand
Posts: 72
Netbeans IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hama Kamal wrote:but if it was like


we could say that

TestIt has-a Dog and TestIt has-a Animal >>> since we can access all the members of Animal class through object reference variable g .... am i right?



My understanding is that when we say HAS-A means whether a class HAS an instance reference to another class. So, to answer your question, i would say that testIt HAS-A Dog, but TestIt does not have an Animal. Dog itself IS-A Animal, but the reference that TestIt has is a DOG reference, so TestIt HAS-A Dog.
 
Hama Kamal
Ranch Hand
Posts: 144
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Boris Mechkov wrote:

Hama Kamal wrote:but if it was like


we could say that

TestIt has-a Dog and TestIt has-a Animal >>> since we can access all the members of Animal class through object reference variable g .... am i right?



My understanding is that when we say HAS-A means whether a class HAS an instance reference to another class. So, to answer your question, i would say that testIt HAS-A Dog, but TestIt does not have an Animal. Dog itself IS-A Animal, but the reference that TestIt has is a DOG reference, so TestIt HAS-A Dog.



well i don't know really know, let's take someone else's idea about this issue,,,,, but one more thing
are you sure that in the below code


TestIt has-a Animal is wrong?! because as i searched some documents i couldn't find that !! it only says that if a class has a rference to another class then it has it ,, it does not say that it shoud be declared outside of the method body,,, my point is are you absolutely sure about that? becasue im not,,, thanks
 
Boris Mechkov
Ranch Hand
Posts: 72
Netbeans IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hama Kamal wrote:

Boris Mechkov wrote:

Hama Kamal wrote:but if it was like


we could say that

TestIt has-a Dog and TestIt has-a Animal >>> since we can access all the members of Animal class through object reference variable g .... am i right?



My understanding is that when we say HAS-A means whether a class HAS an instance reference to another class. So, to answer your question, i would say that testIt HAS-A Dog, but TestIt does not have an Animal. Dog itself IS-A Animal, but the reference that TestIt has is a DOG reference, so TestIt HAS-A Dog.



well i don't know really know, let's take someone else's idea about this issue,,,,, but one more thing
are you sure that in the below code


TestIt has-a Animal is wrong?! because as i searched some documents i couldn't find that !! it only says that if a class has a rference to another class then it has it ,, it does not say that it shoud be declared outside of the method body,,, my point is are you absolutely sure about that? becasue im not,,, thanks



In the code above "Animal a" is local variable/reference. After we are done/exit the method "main()" this reference stops existing. The object might still exists, but not the reference. So, we cant really say that TestIt HAS-A Animal in this case.

This is beside the fact that HAS-A concerns only instance variables, NOT local variable. If "Animal a" was instance variable (one declared outside a method declaration but within a class declaration) then class testIt would be correct to say HAS-A Animal.
What book are you using to study? Kathy and Bert's book on SCJP6 is the best out there.
 
Hama Kamal
Ranch Hand
Posts: 144
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

What book are you using to study? Kathy and Bert's book on SCJP6 is the best out there.



you are right ,, but i didn't see that point in the book,,, Kathy and Bert's book



 
Ash Gill
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks Boris and Hama, so
Has-A relationship only applies to static or instance variables of a class &
Has-A has to do with the reference type of the object and not the actual object type.
 
Hama Kamal
Ranch Hand
Posts: 144
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ash Gill wrote:thanks Boris and Hama, so
Has-A relationship only applies to static or instance variables of a class &
Has-A has to do with the reference type of the object and not the actual object type.




Well I do agree with Boris's explanations they were all logical but yet i'm not sure about those two points i think we need to confirm them through a java text book,, which states those two points explicitly !!!

Hey folks we need a little help over here ......
 
Greenhorn
Posts: 17
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Boris Mechkov wrote:

Hama Kamal wrote:

Ash Gill wrote:hi everyone, i have a small doubt. Given the following code:


can we say that class TestIt has-a Animal and class TestIt has-a Dog?

thanks



Hi Ash,
i thinak we can say that , since TestIt has a reference to class Animal so TestIt has-a Animal and since Dog is-a Animal so TestIt has-a Dog as well....please correct me if you think i'm wrong!



Hama,
you cannot say that TestIt HAS-A animal, nor a Dog. The reference is declared in the main() method, so HAS-A is not valid here. HAS-A is related to instance variables only (variables declared outside a method declarations, but within the class declaration)



Hemakanta,
In this case Dependency Relation is valid. Because instance variable created inside member of class TestIt.
Dependency Relation can be find in two cases-
1. when instance variable created inside member of a class.
2. When object is passing as a parameter to a method or a constructor.
 
Ash Gill
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi sethi,

In one of the practice exams of ocpjp ( k&b practice exam book) i found the explanation that: for Has-a relationship, the instance of another class has to be declared as a member of the class so that it can be part of the state of the class. so in the question i asked, i can confirm, Has-A doesn't exist.
 
reply
    Bookmark Topic Watch Topic
  • New Topic