Forums Register Login

initialization

+Pie Number of slices to send: Send
In the following code, I'm supposed to initialize White.colorName to white.

+Pie Number of slices to send: Send
That code will compile without errors, but it does not do what you think it does.

In class White, you are not initializing the member variable colorName that's in class Color. Instead, you have declared and initialized a new member variable colorName in class White. The member variable colorName from class Color isn't visible in class White, because it's private.

So with your code above, if you create a new White object and call getColor() on it, you will get back null instead of a string containing "White" - because the variable colorName in class Color is null.

If you want to set colorName in class Color to "White", there are several things you could do:

1. Call setColor(...) in the constructor of class White.
2. Make colorName in class Color protected instead of private, and initialize it in the constuctor of class White (you'll have to remove the member variable colorName from class White).
+Pie Number of slices to send: Send
The problem given stated:
The class White inherits from Color, and has its (private) attribute colorName set to White.

I interpreted it to mean the code above. Was I wrong?
Goodbye moon men. Hello tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 541 times.
Similar Threads
Create an Action
A Question on JTabbedPanes
A confused new user of Java that needs some help
Convert Color String to Color?
A confused new user of Java that needs some help
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 16:50:33.