wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes protected access when mixing references and objects Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "protected access when mixing references and objects" Watch "protected access when mixing references and objects" New topic
Author

protected access when mixing references and objects

Caroline Bogart
Greenhorn

Joined: Nov 21, 2001
Posts: 24
thanks so far!
I have many issues understanding references and objects, making this question about protected access that much more confusing.
From Khalid pp. 116-118
<PRE>
// file SuperclassA.java
package packageA;
public class SuperclassA {
public int superclassVarA;
public void superclassMethodA() {}
}
class SubclassA extends SuperclassA {
void subclassMethodA() { superclassVarA = 10; }
}
class AnyClassA {
SuperclassA obj = new SuperclassA();
void anyClassMethodA() {
obj.superclassMethodA();
}
}
// file SubclassB2.java
package packageB;
import packageA.*;
// SubclassB2 extends packageA.SuperclassA
public class SubclassB2 extends SuperclassA {
SuperclassA objRefA = new SubclassB2();
// objRefB reference and object
SubclassB2 objRefB = new SubclassB2();
void subclassMethodB2() {
objRefA.superclassVarA = 10;// no.
objRefB.superclassMethodA();
}
}

</PRE>

The point of this code is to demonstrate that objRefA does not have package access to packageA, but objRefB does have package access to packageA.
I'd first like to get straight exactly what's happening when a subclass object is assigned to a super class reference.
References of type SuperclassA know about the super class, but not about the subclass. So is it fair to say that when assigning a subclass object to a superclass reference, it is safe to use that reference to on superclass members but not subclass members?
I actually have many questions, but let's start there.
Thanks again everyone!

I love JavaRanch!
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
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
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
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.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
Co-author Mike Meyers' Java 2 Certification Passport


Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
Manoj Gupta
Greenhorn

Joined: Oct 31, 2001
Posts: 29
The line in your code that you have commented with a //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

------------------
Cheers,
Manoj
(http://www7.brinkster.com/manoj9/)


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>)
Caroline Bogart
Greenhorn

Joined: Nov 21, 2001
Posts: 24
You are absolutely correct about the stack overflow.
The example is from Khalid ed. 4 p. 117. I wrote to Khalid to inform him of the unfortunate stack overflow effect of the sample code.
And, I apparently mis-wrote the protected SuperclassA, which should be:
<PRE>
public class SuperclassA {
protected int superclassVarA;
protected void superclassMethodA() {}
}
public class SubclassB2 extends SuperclassA {
SuperclassA objRefA = new SubclassB();
SubclassB objRefB = new SubclassB();
void subclassMethodB() {
objRefB.superclassMethodA();
objRefA.superclassVarA = 10;
}
}
</PRE>


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

Caroline Bogart
Greenhorn

Joined: Nov 21, 2001
Posts: 24
OK. If I assign a subclass object to a superclass reference, the subclass (object) IS A superclass and the superclass (reference) IS A superclass. So the reference then knows about superclass members but does not know about subclass members.
Cats have paws
Siamese cats have paws and eyes
Cat cat = new SiameseCat();
cat can reference cat.paws
cat cannot referrence cat.eys
OK?
On the other hand:
Siamese scat = new Cat(); // no
All Siamese cats are cats.
Not all cats are Siamese cats.
Compiler error.
How's this so far?
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]

Caroline Bogart
Greenhorn

Joined: Nov 21, 2001
Posts: 24
You are correct. Please re-read the thread for my correction code, in which the SubclassB2 attempts to access public SuperclassA that has protected members.
My apologies.

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]

Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
hi caroline,
i wonder why that stack overflow occurs??? it's strange. really. i will research on that. 'coz if every machine gets the same exception there is gotta be something we need to know.
bye.
maulin.
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
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
Caroline Bogart
Greenhorn

Joined: Nov 21, 2001
Posts: 24
It's quite a nice bug! As I said, I send it to Khalid. No way to know for sure I have the right address, though.
I'm going to start a new thread on this topic because I posted incorrect code in the first place. Just to be clear.
More on this later.
Caroline
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

 
wood burning stoves
 
subject: protected access when mixing references and objects
 
Similar Threads
Unable to comprehend ......in Khalid Mughal
Protected access modifier
Protected Access Modifier...
accessing protected members
Question on Protected