my dog learned polymorphism*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes 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 "Has-A relationship" Watch "Has-A relationship" New topic
Author

Has-A relationship

Helen Ma
Ranch Hand

Joined: Nov 01, 2011
Posts: 451
I am doing a quiz from Kathy/Bert's study guide:

public class Zing {
Hmpf h;
}
class Woop extends Zing{

}
class Hmpf {Zing z;}

Here are the statments:
a . Woop has a Humpf Woop has a Zing
b . Zing has a Humpf and Woop is a Zing
c . Hump has a Zing and Woop has a Humpf

In my opinion, a is also right because Woop inherits Humpf from Zing. Woop has a Humpf and Humpf has a Zing.
Therefore Woop has a Zing should be right.
But the suggested solution says a is not right.
Can anyone explain to me why?

Joseph Arnold
Ranch Hand

Joined: Oct 05, 2010
Posts: 42
Interesting question!! Inheriting an object is probably different than 'having' an object I guess. When you inherit an variable from a super class, the super class has more control over the variable that is being inherited. For example, If it changes the access control of the variable, the sub class will not be able to access it. Well, I am not sure. I am watching for more answers...
Astha Sharma
Ranch Hand

Joined: Oct 15, 2011
Posts: 245

Inheritance(extending a class) is "Is a " relationship and Aggregation(if you have reference of a class in another) is "has a" relationship. Woop does not "has a" Zing. Woop "is a" Zing.


Astha - OCPJP 6 (90%)
Ankit Gareta
Ranch Hand

Joined: Mar 28, 2011
Posts: 67


As per my understanding....


Now option a is wrong becoz Woop has a Humpf but Woop hasn't a Zing .... if zing extends Hmpf then maybe a will correct...

correct me if i wrong...


OCPJP 6 (91%)
Astha Sharma
Ranch Hand

Joined: Oct 15, 2011
Posts: 245

Ankit Gareta wrote:
As per my understanding....


Now option a is wrong becoz Woop has a Humpf but Woop hasn't a Zing .... if zing extends Hmpf then maybe a will correct...

correct me if i wrong...

Yes its correct
Joseph Arnold
Ranch Hand

Joined: Oct 05, 2010
Posts: 42
Okay, I got it. I was confused over the fact that Woop can still access Zing through Hmpf type.
But since it cannot access it directly i.e 'z' in Hmpf can be accessed only as h.z in Woop, "has-a" Zing option for Woop is ruled out I guess.
Chandraprakash Sarathe
Greenhorn

Joined: Jan 21, 2012
Posts: 16
Now option a is wrong becoz Woop has a Humpf but Woop hasn't a Zing .... if zing extends Hmpf then maybe a will correct...


very interesting question.

So if we have a very little change like below


My doubt is if Hmpf is not inherited can we say that Woop has Hmpf ?


Chandraprakash Sarathe
http://javaved.blogspot.com
Joseph Arnold
Ranch Hand

Joined: Oct 05, 2010
Posts: 42
Ah! The confusion is back again... So the question is whether inheriting a variable different from 'having' the variable??
Oceana Wickramasinghe
Ranch Hand

Joined: Mar 02, 2011
Posts: 77
I think A is correct. Woop is a Zing, Zing has a Hmpf, Hmpf has a Zing, therefor Woop has a Zing.
Oceana Wickramasinghe
Ranch Hand

Joined: Mar 02, 2011
Posts: 77
Joseph Arnold wrote:Ah! The confusion is back again... So the question is whether inheriting a variable different from 'having' the variable??


But the book states that option C is correct, right? And option c points out that Woop has a Hmpf. If that statement is correct then A should also be correct'
Ankit Gareta
Ranch Hand

Joined: Mar 28, 2011
Posts: 67

Hi, Chandraprakash Sarathe

Chandraprakash Sarathe wrote:
Now option a is wrong becoz Woop has a Humpf but Woop hasn't a Zing .... if zing extends Hmpf then maybe a will correct...


very interesting question.

So if we have a very little change like below


My doubt is if Hmpf is not inherited can we say that Woop has Hmpf ?


Here at line 4 if Hmpf h is private then it can't be inherited in Woop class....so in woop class there is no direct reference to the Hmpf class to use method of that class (you have to make object of that class to access it).. so we can't say Woop HAS-A Hmpf

please , correct me if i wronge
Thanks
Raghava Mandagiri
Greenhorn

Joined: Dec 30, 2011
Posts: 9

I am doing a quiz from Kathy/Bert's study guide:

public class Zing {
Hmpf h;
}
class Woop extends Zing{

}
class Hmpf {Zing z;}

Here are the statments:
a . Woop has a Humpf Woop has a Zing
b . Zing has a Humpf and Woop is a Zing
c . Hump has a Zing and Woop has a Humpf

In my opinion, a is also right because Woop inherits Humpf from Zing. Woop has a Humpf and Humpf has a Zing.
Therefore Woop has a Zing should be right.
But the suggested solution says a is not right.
Can anyone explain to me why?



Hi Helen,

question is very nice.

if the code is like below, then option "a" will correct

public class Zing {
Hmpf h = new Hampf();
}

because, reference doen't have "having" sense.
once it become object then we can talk "having" sense.

am not sure, can anybody judge this ?


Regards,
Raghava
Ankit Gareta
Ranch Hand

Joined: Mar 28, 2011
Posts: 67

Hi Raghava Mandagiri ,

Raghava Mandagiri wrote:
I am doing a quiz from Kathy/Bert's study guide:

public class Zing {
Hmpf h;
}
class Woop extends Zing{

}
class Hmpf {Zing z;}

Here are the statments:
a . Woop has a Humpf Woop has a Zing
b . Zing has a Humpf and Woop is a Zing
c . Hump has a Zing and Woop has a Humpf

In my opinion, a is also right because Woop inherits Humpf from Zing. Woop has a Humpf and Humpf has a Zing.
Therefore Woop has a Zing should be right.
But the suggested solution says a is not right.
Can anyone explain to me why?



Hi Oceana,

question is very nice.

if the code is like below, then option "a" will correct

public class Zing {
Hmpf h = new Hampf();
}

because, reference doen't have "having" sense.
once it become object then we can talk "having" sense.

am not sure, can anybody judge this ?


if reference dosen't have HAS-A sense....then all the option is wronge because in the code there is no object initialization.

so , i think bellow is wronge..
because, reference doen't have "having" sense.
once it become object then we can talk "having" sense.
Oceana Wickramasinghe
Ranch Hand

Joined: Mar 02, 2011
Posts: 77
class Zing {
Hmpf h;
}
class Woop extends Zing{
void doStuff(){
System.out.println(h.z);
}
}
class Hmpf {Zing z;}

This works perfectly fine, a should be correct.
Raghava Mandagiri
Greenhorn

Joined: Dec 30, 2011
Posts: 9

Dear Ankit,

thanks for quick response.

reference doen't have "having" sense.
once it become object then we can talk "having" sense.


am saying these lines in reachability perspective.

Woop has a Humpf Woop has a Zing

here "Woop has a Humpf" it's absalutely correct
but it's not possible to reach Zing fron Woop. because Humpf is not an object.
if Humpf is an object then we will reach Zing as Woop --> Humpf --> Zing. right ?

Oceana Wickramasinghe
Ranch Hand

Joined: Mar 02, 2011
Posts: 77
Raghava Mandagiri wrote:
but it's not possible to reach Zing fron Woop. because Humpf is not an object.
if Humpf is an object then we will reach Zing as Woop --> Humpf --> Zing. right ?



Then how does the code in my previous post compile? You can access whatever is in the Hmpf class though a Hmpf reference variable.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4343
    
    8

Oceana Wickramasinghe wrote:I think A is correct. Woop is a Zing, Zing has a Hmpf, Hmpf has a Zing, therefor Woop has a Zing.

The second bit is the problem - "HAS-A" is not transitive.

If I have a brother, and my brother has a car, that does not mean I have a car.
Raghava Mandagiri
Greenhorn

Joined: Dec 30, 2011
Posts: 9

class Zing {
Hmpf h;
}
class Woop extends Zing{
void doStuff(){
System.out.println(h.z);
}
}
class Hmpf {Zing z;}


is it executing fine ?

it must cause "NullPointerException" (h.z) // h is null.
here 'z' is not reachable. (memory is not allocated for z)
Oceana Wickramasinghe
Ranch Hand

Joined: Mar 02, 2011
Posts: 77
Raghava Mandagiri wrote:
is it executing fine ?

it must cause "NullPointerException" (h.z) // h is null.
here 'z' is not reachable. (memory is not allocated for z)


If we changed it to Hmpf h=new Hmpf(); ?

This raises another question. We say that Woop has a Hmpf because Zing has a Hmpf reference variable and woop extends Zing. Do we say this hypothetically, because how can there be a variable without an object, it is just typed, but inside jvm theres no variable is there?
Oceana Wickramasinghe
Ranch Hand

Joined: Mar 02, 2011
Posts: 77
ok i think i got it, you can say that a class 'HAS' something, only if you're able to access the given variable directly or, using an reference variable of the same class. In this example Woop can not directly access the Zing variable or access it using a reference variable of Woop, you're required to use Hmpf. Therefor its not a HAS A relationship.
John Stark
Ranch Hand

Joined: Jul 19, 2011
Posts: 185
Question 54 of the first practice exam of the K & B Practice Exam book is:



In the answer it says: Glanks have Bostrons indirectly through Jookers. (And saying Glanks hava a Bostron is correct). So here accessing a class indirectly is considered a has-a relationship.
Here Glanks have the Jookers directly and the Jookers have Bostrons.
In the example discussed here Woop has a Zing via inheritance and Zing has-a Hmpf. Is it then justified saying there is no has-a relationship? I am highly confused

John
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8801
    
    5
No matter what the final conclusion is, it's wonderful to see everyone using these strange words so effortlessly.


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
Helen Ma
Ranch Hand

Joined: Nov 01, 2011
Posts: 451
This is a mock exam question from Mughal and Ras's book:
According to this book has a relationship is transitive. But I think this has-a relationship can be subjective too.
It depends on how you design your software. It the has-a relationship means composition or aggregation, that the has-a relationship can be transitive.
For example, if Car is composed of wheels and wheels is composed of tires, then Car has-a tires. Let me use the previous example. If I have a brother and my brother has a Car. I and a brother object does not have composite relationship, then I do not have has-a relationship with Car.

Correct me if I am wrong.


class Foo {
int num;
Baz comp = new Baz();
}
class Bar {
boolean flag;
}
class Baz extends Foo {
Bar thing = new Bar();
double limit;
}

Select the three correct answers.
(a) A Bar is a Baz.
(b) A Foo has a Bar.
(c) A Baz is a Foo.
(d) A Foo is a Baz.
(e) A Baz has a Bar.
(b), (c), and (e)
An instance of the class Baz is also an instance of the class Foo, since the class Baz
extends the class Foo. A Baz has a Bar since instances of the class Baz contain an
instance of the class Bar by reference. A Foo has a Baz, since instances of the class Foo
contain an instance of the class Baz by reference. Since a Foo has a Baz which has a
Bar, a Foo has a Bar.
 
Consider Paul's rocket mass heater.
 
subject: Has-A relationship
 
Similar Threads
why do I have this red X - using eclipse (image included)
one mock exam question from K&B book
mock exam doubt has-a relationship
is-a and has-a relation and inheritance
Not convinced with K&B solution for couple of questions, please help