I love JavaRanch!
Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
Cheers, <img src="smile.gif" border="0"> <br /><a href="http://www7.brinkster.com/manoj9/" target="_blank" rel="nofollow">Manoj</a><br />(<a href="http://www7.brinkster.com/manoj9/" target="_blank" rel="nofollow">http://www7.brinkster.com/manoj9/</a>)
Originally posted by Maulin, Vasavada:
hi,
sorry but i didnt understand ur point. i tried similar code and got stack overflow exception. and where u use protected which u mentioned in first part of ur mail?
can u clear my doubts?
thanks
maulin
I love JavaRanch!
Originally posted by Jane Griscti:
[B]Hi Caroline,
Yes, a subclass can always be assigned to a superclass reference.
A subclass is a specialization of its superclass; it will have all the characteristics of the superclass plus some additional characteristics. For example, in
It will always be true that a SiameseCat is a Cat, it will always have 'paws', so you can always assign a Siamese object to a Cat reference.
However, it will not always be true that a Cat is a SiameseCat.
The declared contract for the SiameseCat class guarantees that all SiameseCat objects will have an attribute 'eyes'. A Cat object does not have that attribute; it would break the SiameseCat contract; so the compiler won't allow you to assign a Cat to a SiameseCat reference.
Hope that helps.
[/B]
I love JavaRanch!
Originally posted by Manoj Gupta:
The line in your code that you have commented with a [b]//no will work.
objRefA.superclassVarA = 10; //no.
Reference objRefA points to a SubclassB2 object (Superclass references can legally point to a subclass object in Java), which is extending SuperclassA with a public member variable superclassVarA. Public member variables can be accessed with an object.variable notation anywhere.
To see it yourself compile and execute these:
SuperclassA.java
SubclassB2.java:
Compile using
javac -d . *.java
and execute using
java packageB.SubclassB2
[/B]
I love JavaRanch!
Originally posted by Maulin, Vasavada:
Hi caroline,
how stupid of me!!! its obvious. it will result into overflow stack exception for following reason,
we r trying to create object of type SubclassB2 in SubclassB2 while writing SuperclassA objrefA = new SubclassB2();
jvm encounters this it tries to create SubclassB2's Object where it encounters the same statement again and again...
it goes on forever...
so...that's how its getting that exception,
oh! man i shd 've recognized it at first sight.
regards,
maulin
I love JavaRanch!
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime. |