• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

basic OO question

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 1071
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
{}
 
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic