This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes The hard problem about Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "The hard problem about "is a ,has a",and anonymous class,please clearify?" Watch "The hard problem about "is a ,has a",and anonymous class,please clearify?" New topic

The hard problem about "is a ,has a",and anonymous class,please clearify?

Tu Ran
Ranch Hand

Joined: Feb 09, 2002
Posts: 30
what' the relation 'is a and has a';

What is the relationship between Movable and Bitmap?
public interface Moveable
public void moveObject();
public class Bitmap implements Moveable
public void moveObject()
//body omitted

A. It's a has a relationship.

B. It's an is a relationship.

C. It's both an is a and has a relationship.

D. The relationship is neither an is a nor a has a relationship.

public class Building {
int a = 10;
int c = 30;
static int b;
public Runnable getRunnable() {

final int a = 20;
return new Thread() {
public void run(){}
public void xx() {b = a+c; }
public static void main(String[]args){
Building xx=new Building();
}//why b is 0 ?i think the xx()have run?right?

Enjoy java,enjoy life
Junilu Lacar

Joined: Feb 26, 2001
Posts: 4442

"is a" - look for the extends and implements keywords.
interface I {}
class A {}
class B extends A {}
class C extends B {}
class D implements I {}
With the above declarations, the following can be said:
B "is a" kind/specialization/subclass of A
C "is a" kind/specialization/subclass of A and B
D "is an" implementation of I
"has a" - look at class members.
class A {}
class B {
A a;
You can say that class B has/contains an instance of class A

In the first question you posed, you can say that Bitmap is a Moveable since it implements that interface.

Junilu Lacar

Joined: Feb 26, 2001
Posts: 4442

As for your second question (and please, if you have multiple and unrelated questions, it's preferable that you post each in a separate thread):
The method xx is never executed. It is the local variable xx in main() that is actually used. Invoking the getRunnable() method on xx simply returns an instance of the anonymous class, it doesn't start it. Therefore, the run() method in the anonymous class has not been executed. Besides that, there is nothing in the code that actually invokes the xx() method in the anonymous class. So, b remains unchanged with its initial value.
Rajinder Yadav
Ranch Hand

Joined: Jan 18, 2002
Posts: 178
An 'is-a' relationship has to do with subclassing when talking about other classes, or implementation when speaking about interfaces
interface I {}
class A{}
class B extends A implements I {}
here we can say
class B 'is-a' subclass of class A
class B 'is-a' implementation of interface I
One way to check an is-a relationship is to use the instanceof operator,
using the above classes, if:
B b = new B();
then the boolean value of:
(b instanceof A) will be true
(b instanceof I) will be true
An 'has-a' relationship refers to containment, where one object contains another object, not to be confused with inner-classes

I the above code, we can see that class B contains a reference of type class A, so we can say that class B 'has-a' class A relationship
NOTE: the 'has-a' relationship has to do with containment of reference types and not java primitive types!!!
So in the previous code we can't say class B 'has-a' char, even though it contains a char field 'c'

<a href="" target="_blank" rel="nofollow">Rajinder Yadav</a><p>Each problem that I solved became a rule which served afterwards to solve other problems. --Rene Descartes
Tu Ran
Ranch Hand

Joined: Feb 09, 2002
Posts: 30
Thanks a lots my friends in java~~~studing road~~~~
I agree. Here's the link:
subject: The hard problem about "is a ,has a",and anonymous class,please clearify?
Similar Threads
ID:12 SCJP Question of the Day
tricks and trips
How to make all my graphics selectable and doubleclick with event?