Solved

Sibling Reference

Posted on 2004-04-01
13
243 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
 
LVL 30

Expert Comment

by:mayankeagle
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
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:mayankeagle
ID: 10740371
Guess that will throw a ClassCastException.
0
 
LVL 30

Expert Comment

by:mayankeagle
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:mayankeagle
ID: 10740555
>> You probably not have seen the comma separating declarations of Parent references

Right, I missed the comma :-)
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
bigHeights  challenge 13 55
noX challenge 17 76
nestparen challenge 4 55
mapAB Challlenge 35 84
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
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 one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

708 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

14 Experts available now in Live!

Get 1:1 Help Now