Jump to content

  • Log In with Google      Sign In   
  • Create Account


[?][JAVA] class extends


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 0Circle0   Members   -  Reputation: 343

Like
0Likes
Like

Posted 31 December 2012 - 05:07 AM

Why can a class only have one parent but you can chain parents together effectivly doing the same thing like this? Or am I thinking this is doing something that it isn't?

 

public class myFirstClass
{
    public int someFirstInt;
    public myFistClass(int fi)
    {
        someFirstInt = fi;
    }
}
public class mySecondClass extends myFirstClass
{
    public int someSecondInt;
    public mySecondClass(int si, int fi)
    {
        super(fi);
        someSecondInt = si;
    }
}
public class myThirdClass extends mySecondClass
{
    public int someThirdInt;
    public myThirdClass(int ti, int si, int fi)
    {
        super(si, fi);
        someThirdInt = ti;
    }
}
public class myForthClass extends myThirdClass
{
    public int someForthInt;
    public myForthClass(int foi, int ti, int si, int fi)
    {
        super(ti, si, fi);
        someForthInt = foi;
    }
}
myForthClass ChainParent = new myForthClass(1, 1, 1, 1);

does myForthClass effectivly have access to everything in myThirdClass, mySecondClass, and myFirstClass?


Edited by 0Circle0, 31 December 2012 - 05:08 AM.

Sprite Creator 3 VX & XP

WARNING: I edit my posts constantly.


Sponsor:

#2 RulerOfNothing   Members   -  Reputation: 1160

Like
5Likes
Like

Posted 31 December 2012 - 06:12 AM

does myForthClass effectivly have access to everything in myThirdClass, mySecondClass, and myFirstClass?
Yes it does. In answer to your first question, the capability of an object having multiple 'parents' is known as multiple inheritance and Java does not have this capability because multiple inheritance can make object-oriented code difficult to reason about, especially due to what is known as the "diamond problem" (where class A derives from classes B and C, which both derive from class D, making it somewhat ambiguous as to what version of A's members that are being inherited from D are being referred to)

#3 minibutmany   Members   -  Reputation: 1592

Like
1Likes
Like

Posted 31 December 2012 - 08:26 AM

Building on that, a good example is JFrame and applet. You can't extend to both a JFrame and an applet because it is impossible to be both, they both have properties that cannot be shared.
Stay gold, Pony Boy.

#4 0Circle0   Members   -  Reputation: 343

Like
0Likes
Like

Posted 31 December 2012 - 11:15 AM

Thanks for the info.


Sprite Creator 3 VX & XP

WARNING: I edit my posts constantly.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS