Omnimaga

General Discussion => Technology and Development => Computer Programming => Topic started by: ElementCoder on December 10, 2012, 03:08:56 pm

Title: Inheritance
Post by: ElementCoder on December 10, 2012, 03:08:56 pm
So I am in need of a little help here. There is the following problem:
Look at the following code. Assume there are five types (classes or interfaces) (U, G, B, Z and X) and one variable with each type.
Code: [Select]
U u;
G g;
B b;
Z z;
X x;
The following assignments are allowed (they compile):
Code: [Select]
u = z;
x = b;
g = u;
x = u;
The following assignments are NOT allowed:
Code: [Select]
u = b;
x = g;
z = u;
g = x;
The question:
What can you say, with the information you've gotten, about the types and their relations (how do they relate to each other?)

I'm pretty confused here so any help would be appreciated :)
Title: Re: Inheritance
Post by: pimathbrainiac on December 10, 2012, 03:16:00 pm
class U is a subclass of class Z
class X is a subclass of class B
class G is a subclass of class U
class X is a subclass of class U

class U is not a subclass of B
class X is not a subclass of G
class Z is not a subclass of U
class G is not a subclass of X
Title: Re: Inheritance
Post by: cooliojazz on December 10, 2012, 03:19:32 pm
You have your logic backwards pimathbrain.  And Element, I've run into a problem, with the z, x, and u, it seems to me to be impossible for those three conditions to be true (or false as it were).  I'll see if I can't figure something out though =P
Title: Re: Inheritance
Post by: pimathbrainiac on December 10, 2012, 03:20:06 pm
You have your logic backwards pimathbrain.  And Element, I've run into a problem, with the z, x, and u, it seems to me to be impossible for those three conditions to be true (or false as it were).  I'll see if I can't figure something out though =P

I wasn't sure of my logic, anyways
Title: Re: Inheritance
Post by: Runer112 on December 10, 2012, 03:28:39 pm
I think there are three possible solutions to this problem using only the given classes/interfaces. They have the same basic hierarchy: X is a parent of B, X and G are parents of U, and U is a parent of Z. The three solutions come from the fact that either one or both of G and X can be an interface. Here are the three solutions (I think):





EDIT: Fixed, maybe? Why does this hurt my brain so much...
Title: Re: Inheritance
Post by: ElementCoder on December 10, 2012, 03:53:52 pm
Thanks for your help guys. With your help I've figured out what it should be:
Code: [Select]
class U implements X, G
interface G
interface X
class B implements X
class Z extends U
[edit] aww runer you ninja'd me
Thanks :D ;D(http://www.omnimaga.org/Themes/default/images/gpbp_arrow_up.gif)
(http://4.bp.blogspot.com/_D_Z-D2tzi14/S8TRIo4br3I/AAAAAAAACv4/Zh7_GcMlRKo/s400/ALOT.png)