?
Solved

Sibling Reference

Posted on 2004-04-01
13
Medium Priority
?
251 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

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!

Question has a verified solution.

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

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Suggested Courses
Course of the Month9 days, 21 hours left to enroll

762 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