?
Solved

Sibling Reference

Posted on 2004-04-01
13
Medium Priority
?
255 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 3
  • +2
13 Comments
 
LVL 20

Accepted Solution

by:
Venabili earned 1050 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
Technology Partners: 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!

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

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 second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

649 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