Flurite
|
  |
| Joined: 03 Apr 2011 |
| Total Posts: 5386 |
|
|
| 24 Jun 2012 07:39 AM |
Hello,
Some of you may know about the diamond problem in languages that support multiple inheritance. Well, basically, it's where you have a tier-3 class, with two direct-child, tier-2 classes that inherit from it, and then a tier-1 class that inherits from both the tier-2 classes. That causes problems because two tier-3 classes will be created. One from each of the tier-2 classes!
Now in all the tutorials I read about, they go on bla-bla-bla about how you can solve problems about ambiguous members, etc. They talk about scope resolution and then they get to virtual base classes. They simply put two "virtual"s in front of the public and say now one tier-3 class exists.
That's not very explanatory. A person like me asks: "What if only one of those tier-2 classes are inherited virtually, and the other one is inherited regularly?" So I did some tests:
(ideone) /QDYyN --this is the regular version with both tier-2 classes inherited virtually
(ideone) /DOgjp --this is WEIRD
(ideone) /oqc7A --as if there were no virtuals!
|
|
|
| Report Abuse |
|
|
Flurite
|
  |
| Joined: 03 Apr 2011 |
| Total Posts: 5386 |
|
|
| 24 Jun 2012 07:40 AM |
| My question is how did the second to outputs come to be? |
|
|
| Report Abuse |
|
|
Consitor
|
  |
| Joined: 01 Feb 2012 |
| Total Posts: 3691 |
|
| |
|
|
| 24 Jun 2012 09:00 AM |
| This is why Java doesn't allow for you to inherit from multiple classes, only interfaces. |
|
|
| Report Abuse |
|
|
NXTBoy
|
  |
| Joined: 25 Aug 2008 |
| Total Posts: 4533 |
|
|
| 24 Jun 2012 09:40 AM |
| None of your tests should produce any output! Can you add a `main` method to them so we can see what you're actually doing, and run the code in IDEone? |
|
|
| Report Abuse |
|
|
Flurite
|
  |
| Joined: 03 Apr 2011 |
| Total Posts: 5386 |
|
|
| 24 Jun 2012 10:22 AM |
@NXTBoy,
WILL DO.
(ideone) /2qkD2 --remake of first one (ideone) /TOB4I --remake of second one (ideone) /eH8qx |
|
|
| Report Abuse |
|
|
Flurite
|
  |
| Joined: 03 Apr 2011 |
| Total Posts: 5386 |
|
|
| 24 Jun 2012 10:31 AM |
| no wait, you want me to create a class, and show you the legit output or wat |
|
|
| Report Abuse |
|
|
Flurite
|
  |
| Joined: 03 Apr 2011 |
| Total Posts: 5386 |
|
|
| 24 Jun 2012 12:20 PM |
Hmm.. it always seems like I can't speak English. What do you mean by `main` method?
I just showed you the output in IDEone by creating a fox in main() |
|
|
| Report Abuse |
|
|
Quenty
|
  |
| Joined: 03 Sep 2009 |
| Total Posts: 9316 |
|
|
| 24 Jun 2012 12:50 PM |
Does anyone else only understand a few words in that jumble of words?
Or am I just ignorant? |
|
|
| Report Abuse |
|
|
Flurite
|
  |
| Joined: 03 Apr 2011 |
| Total Posts: 5386 |
|
| |
|
Flurite
|
  |
| Joined: 03 Apr 2011 |
| Total Posts: 5386 |
|
|
| 24 Jun 2012 01:16 PM |
| and I am not that good at describing thingies. |
|
|
| Report Abuse |
|
|
Flurite
|
  |
| Joined: 03 Apr 2011 |
| Total Posts: 5386 |
|
|
| 24 Jun 2012 01:24 PM |
I'll illustrate:
______A N I M A L_______ -- TIER 3 _____/__________\_______ FourLegs______Mammal --TIER 2 ____\___________/______ _________F O X ____________ -- TIER 1
Notice how Animal, FourLegs, and Mammal and Fox form a diamond shape? See, in inheritance, FourLegs and Mammal inherits stuff from Mammal. Then, Fox inherits from FourLegs AND Mammal. Whatta you know, you got two Animal classes in Fox!
|
|
|
| Report Abuse |
|
|
|
| 24 Jun 2012 01:42 PM |
| The number of legs an animal has should be a property anyway... >.> |
|
|
| Report Abuse |
|
|
Flurite
|
  |
| Joined: 03 Apr 2011 |
| Total Posts: 5386 |
|
| |
|
|
| 24 Jun 2012 01:49 PM |
| Stop inheriting multiple classes if you really want to solve it. Seriously. |
|
|
| Report Abuse |
|
|
Flurite
|
  |
| Joined: 03 Apr 2011 |
| Total Posts: 5386 |
|
|
| 24 Jun 2012 01:52 PM |
| I am learning the language of C++. I feel like inheriting from different sources can be helpful. |
|
|
| Report Abuse |
|
|
|
| 24 Jun 2012 01:54 PM |
| Don't you think there was a reason behind Java inheriting C++'s syntax in many cases, whilst keeping multiple inheritance out? |
|
|
| Report Abuse |
|
|
Flurite
|
  |
| Joined: 03 Apr 2011 |
| Total Posts: 5386 |
|
|
| 24 Jun 2012 02:03 PM |
Yeah, the reason is because Java wanted to be weaker than C++. That's why C++ is more powerful. Even Wikipedia says so.
After all, even though some features may not be used often, it sometimes can be helpful just to leave it there. And then have solutions to it, like this. |
|
|
| Report Abuse |
|
|
|
| 24 Jun 2012 02:08 PM |
C++ is more powerful because it has more access to the hardware. Java is abstracted from the hardware and is therefore cross-platform, but at a somewhat significant power loss.
*whacks Flurite over the head with a cast-iron frying pan* |
|
|
| Report Abuse |
|
|
Flurite
|
  |
| Joined: 03 Apr 2011 |
| Total Posts: 5386 |
|
|
| 24 Jun 2012 02:12 PM |
| Sure because it's not about virtual base classes? |
|
|
| Report Abuse |
|
|
|
| 24 Jun 2012 02:13 PM |
| Java class inheritance looks like a tree. C++ class inheritance looks like spaghetti. :/ |
|
|
| Report Abuse |
|
|
myrkos
|
  |
| Joined: 06 Sep 2010 |
| Total Posts: 8072 |
|
|
| 24 Jun 2012 02:16 PM |
| C++ inheritance looks however you want. |
|
|
| Report Abuse |
|
|
|
| 24 Jun 2012 02:18 PM |
@myrkos
SPAGHETTI!!!
Btw, did you hear the Java 8 is coming with closures? |
|
|
| Report Abuse |
|
|
Flurite
|
  |
| Joined: 03 Apr 2011 |
| Total Posts: 5386 |
|
|
| 24 Jun 2012 02:19 PM |
Looks like a diamond to me.
! |
|
|
| Report Abuse |
|
|
myrkos
|
  |
| Joined: 06 Sep 2010 |
| Total Posts: 8072 |
|
|
| 24 Jun 2012 02:37 PM |
"Btw, did you hear the Java 8 is coming with closures?"
C++ already has that. BUHAHA |
|
|
| Report Abuse |
|
|