wood burning stoves 2.0*
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 OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP 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
Author

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

Tu Ran
Ranch Hand

Joined: Feb 09, 2002
Posts: 30
fist:
what' the relation 'is a and has a';
e.g:
QUESTION 19/38:

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.

---second:
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();
xx.getRunnable();
System.out.println(b);
}//why b is 0 ?i think the xx()have run?right?


Enjoy java,enjoy life
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4777
    
    8

"is a" - look for the extends and implements keywords.
Example:
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.
Example:
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.

HTH,
Junilu
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4777
    
    8

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.
Junilu
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="http://www.rajindery.com" 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: http://aspose.com/file-tools
 
subject: The hard problem about "is a ,has a",and anonymous class,please clearify?