• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"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
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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'
 
Tu Ran
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lots my friends in java~~~studing road~~~~
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic