[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 260
  • Last Modified:

Sibling Reference

Hi:
    The following code indicates that I cannot assign an object to a sibling reference. Why??

class Parent
{

}

class DerivedOne extends Parent
{
}

class DerivedTwo extends Parent
{
}

class Test
{  public static void main (String args[])
   {  Parent p = new Parent();
     
      DerivedOne d1 = new DerivedOne();
     
      DerivedTwo d2 = new DerivedTwo();
     
      d1 =(DerivedOne) d2;
   
   }
   
   
}  


0
LearningJava
Asked:
LearningJava
  • 4
  • 3
  • 3
  • +2
1 Solution
 
VenabiliCommented:
It is normal.

DerivedOne and DerivedTwo are extending Parent but they both are different classes with their own methods.

You can cast a Parent object to any of these two... but you can't cast them to each other. Otherwise we can cast any object to any other - all are extending class Object :)
0
 
VenabiliCommented:
Read this for better understanding of inheritance:
http://java.sun.com/docs/books/tutorial/java/concepts/inheritance.html
0
 
zzynxSoftware engineerCommented:
Indeed.

imagine:

class Parent
{
  private String data;
}

class DerivedOne extends Parent
{
  private int dataOne;
}

class DerivedTwo extends Parent
{
  private String dataTwo;
}

DerivedOne d1 = new DerivedOne();

d1 has as members:
1) a String: data
2) an int: dataOne
     
DerivedTwo d2 = new DerivedTwo();

d2 has two members
1) a String: data
2) a String: dataTwo

It's clear that if you try cast d2 to a DerivedOne object, there's a problem with the 2nd data member.

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Mayank SAssociate Director - Product EngineeringCommented:
>> Otherwise we can cast any object to any other - all are extending class Object

Correct. It will throw ClassCastExceptions. You can assign a reference to an object only if it is an instance of it. You can check it, to avoid the exception:

if ( d2 instanceof DerviedOne )
  d1 = ( DerviedOne ) d2 ; // end if

else
  System.out.println ( "d2 is not an instance of DerivedOne class. " ) ; // end if
0
 
StillUnAwareCommented:
this works:

public class Parent{
}

class DerivedOne extends Parent{
}

class DerivedTwo extends Parent{
}

class Test{
  public static void main (String args[]) {
    Parent p = new Parent();
    DerivedOne d1 = new DerivedOne();
    DerivedTwo d2 = new DerivedTwo();
    d1 =(DerivedOne)((Parent)d2);
  }
}  
0
 
StillUnAwareCommented:
First you upcast to base class Parent, then downcast to DerivedOne.
0
 
StillUnAwareCommented:
I am really sory for my unawareness, I made a mistake :). It won't  help you.
0
 
WebstormCommented:
You can do this:

class Test{
  public static void main (String args[]) {
    Parent p = new Parent(),
         d1 = new DerivedOne(),
         d2 = new DerivedTwo();
    d1 =d2;
  }
}  


0
 
Mayank SAssociate Director - Product EngineeringCommented:
Guess that will throw a ClassCastException.
0
 
Mayank SAssociate Director - Product EngineeringCommented:
I mean: >> d1 =d2;
0
 
WebstormCommented:
No, because d1 and d2 are Parent instance reference and DerivedOne & DerivedTwo are subclass of Parent.
Have you tested it ?
0
 
WebstormCommented:
>> d1 and d2 are Parent
You probably not have seen the comma separating declarations of Parent references.

0
 
Mayank SAssociate Director - Product EngineeringCommented:
>> You probably not have seen the comma separating declarations of Parent references

Right, I missed the comma :-)
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

  • 4
  • 3
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now