Solved

Sibling Reference

Posted on 2004-04-01
13
245 Views
Last Modified: 2008-03-06
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
Comment
Question by:LearningJava
  • 4
  • 3
  • 3
  • +2
13 Comments
 
LVL 20

Accepted Solution

by:
Venabili earned 350 total points
ID: 10732044
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
 
LVL 20

Expert Comment

by:Venabili
ID: 10732081
Read this for better understanding of inheritance:
http://java.sun.com/docs/books/tutorial/java/concepts/inheritance.html
0
 
LVL 37

Expert Comment

by:zzynx
ID: 10732131
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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 30

Expert Comment

by:Mayank S
ID: 10732162
>> 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
 
LVL 14

Expert Comment

by:StillUnAware
ID: 10733631
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
 
LVL 14

Expert Comment

by:StillUnAware
ID: 10733652
First you upcast to base class Parent, then downcast to DerivedOne.
0
 
LVL 14

Expert Comment

by:StillUnAware
ID: 10733721
I am really sory for my unawareness, I made a mistake :). It won't  help you.
0
 
LVL 13

Expert Comment

by:Webstorm
ID: 10740030
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
 
LVL 30

Expert Comment

by:Mayank S
ID: 10740371
Guess that will throw a ClassCastException.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10740384
I mean: >> d1 =d2;
0
 
LVL 13

Expert Comment

by:Webstorm
ID: 10740500
No, because d1 and d2 are Parent instance reference and DerivedOne & DerivedTwo are subclass of Parent.
Have you tested it ?
0
 
LVL 13

Expert Comment

by:Webstorm
ID: 10740533
>> d1 and d2 are Parent
You probably not have seen the comma separating declarations of Parent references.

0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10740555
>> You probably not have seen the comma separating declarations of Parent references

Right, I missed the comma :-)
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now