aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes basic OO question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "basic OO question" Watch "basic OO question" New topic
Author

basic OO question

gayathri mukkavilli
Greenhorn

Joined: Mar 09, 2005
Posts: 20
Hi,
I got a very basic question. Look at the below code.

class Mytest
{
Mytest a;
Mysubtest b;
a= new Mytest();
b= new Mysubtest();
a=b;
// remaining code;
}
class Mysubtest extends Mytest
{}

What happens at a==b statement. Here a and b are both reference variables and if value of b is putting into a, does a and b points to Mysubtest? or a points to Mytest and b points to Mysubtest? and Why?
Steven Bell
Ranch Hand

Joined: Dec 29, 2004
Posts: 1071
After you do
a=b;

a and b are references to the same object (the Mysubtest object) so a==b will return true.
gayathri mukkavilli
Greenhorn

Joined: Mar 09, 2005
Posts: 20
Hi,
Then again after a=b if you give another statement b=a, it is giving error.
if a and b are both refer to the same object then we can give b=a also after a=b, right? Why it is giving error?

class Mytest
{
Mytest a;
Mysubtest b;
a= new Mytest();
b= new Mysubtest();
a=b;
b=a; //giving compile error
// remaining code;
}
class Mysubtest extends Mytest
{}

Thanks for the immediate reply
sethu chiyan
Greenhorn

Joined: Apr 05, 2005
Posts: 10
Hi Gayathri,

As far as I know you can copy the reference variable of child class to parent class's.Child class reference variable can b assigned to any class reference variable of higher in the hierarchy.

About ERR: Compiler follows the hierarchy reflection and it can assure itself a legal conversion. So u cant assign wise versa.
sethu chiyan
Greenhorn

Joined: Apr 05, 2005
Posts: 10
class Mytest
{
Mytest a;
Mysubtest b;
a= new Mytest();
b= new Mysubtest();
a=b; //Compiler can assure itself a legal conversion.
b=a; //giving compile error (Yes!) coz Compiler follows the hierarchy reflection. u can't do it wise versa
// remaining code;
}
class Mysubtest extends Mytest
{}
Amit Das
Ranch Hand

Joined: Mar 05, 2005
Posts: 206
hi Gayathri,



see when you write a = b; actually a is refering to the Mysubtest object only but its type is still of type Mytest only. Now when you write b = a;
you are trying to downcast here without explicit cast as b's type is still Mysubtest and a's type is still Mytest. So for getting a compile-time error free code we have to do a type cast here as you can see in the code.
hope this helps...

amit
James Christian
Ranch Hand

Joined: Apr 04, 2005
Posts: 63
I concur. The solution is to cast. The JVM has no way of knowing what an objects sub-classes can be so if it doesn't find the object to be of the same type or of one of its supertypes it can't do the asignment. This is a protection against assignments of incompatible types.

e.g

abstract class Fruit{}
public class Banana extends Fruit{}

we can say that Banana is a fruit because Fruit is Banana's superclass but we can't know for sure if a Fruit object is a Banana because maybe it's an apple.
 
 
subject: basic OO question