Author Topic: Inheritance  (Read 3923 times)

0 Members and 1 Guest are viewing this topic.

Offline ElementCoder

  • LV7 Elite (Next: 700)
  • *******
  • Posts: 611
  • Rating: +42/-2
    • View Profile
Inheritance
« 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 :)

Some people need a high five in the face... with a chair.
~EC

Offline pimathbrainiac

  • Occasionally I make projects
  • Members
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1731
  • Rating: +136/-23
  • dagaem
    • View Profile
Re: Inheritance
« Reply #1 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
I am Bach.

Offline cooliojazz

  • Support Staff
  • LV7 Elite (Next: 700)
  • *******
  • Posts: 619
  • Rating: +66/-9
  • I omnoms on your soul
    • View Profile
    • Unreal Phantasies
Re: Inheritance
« Reply #2 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
Spoiler For Random signess:
You can not beat my skills.
Trust me.
So don't even try.
And remember never to trust someone who says, "Trust me."

TI File Editor Progress: Remade in java like a boss. 50% we'll call it? IDK =P
Java Libraries: JIRC - 90% JTIF - 5%
TI Projects: Unreal Notator - -5000%
Nomcraft, a Bukkit mod
Some of the music I write can be found here | The Rest Should Be Here (Bandcamp)

Offline pimathbrainiac

  • Occasionally I make projects
  • Members
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1731
  • Rating: +136/-23
  • dagaem
    • View Profile
Re: Inheritance
« Reply #3 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
I am Bach.

Offline Runer112

  • Moderator
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2289
  • Rating: +639/-31
    • View Profile
Re: Inheritance
« Reply #4 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):

  • class G
  • interface X
  • class B implements X
  • class U extends G implements X
  • class Z extends U

  • interface G
  • class X
  • class B extends X
  • class U extends X implements G
  • class Z extends U

  • interface G
  • interface X
  • class B implements X
  • class U implements G, X
  • class Z extends U


EDIT: Fixed, maybe? Why does this hurt my brain so much...
« Last Edit: December 10, 2012, 03:52:45 pm by Runer112 »

Offline ElementCoder

  • LV7 Elite (Next: 700)
  • *******
  • Posts: 611
  • Rating: +42/-2
    • View Profile
Re: Inheritance
« Reply #5 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
« Last Edit: December 10, 2012, 04:01:04 pm by ElementCoder »

Some people need a high five in the face... with a chair.
~EC