Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

protected access when mixing references and objects

 
Caroline Bogart
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Manoj Gupta
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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/)
 
Caroline Bogart
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic