GeeCON Prague 2014*
The moose likes Beginning Java and the fly likes Confused with the assignment operator! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Confused with the assignment operator!" Watch "Confused with the assignment operator!" New topic
Author

Confused with the assignment operator!

Ashish Dutt
Ranch Hand

Joined: Apr 12, 2013
Posts: 172



Dear friends,
Im kinda dumbfounded with the above code. Why is it that the reference variable super1 of SuperClassExp not able to access the instance variable of subClassExp even though it has been assigned to it using the assignment operator and the new keyword. Its only able to access the instance variable declared in its own class. why im all the more confused is that the assignment operator is supposed to assign a value to the varaible.
Now in this case why is the reference variable of a superclass not able to access the variables of the subclass even when it has been assigned to it?

Your detailed and clear reasoning would be well appreciated.

Still a beginner at crafting quality coding but have the zeal to learn more. Your help would be appreciated.
My Blog on Learning Analytics
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4397
    
    8

As things stand, that won't compile (because you haven't declared the sub variable). Show us some code that will compile and the output you get from it (or errors, if there are any), and we can explain what's going on.
Ashish Dutt
Ranch Hand

Joined: Apr 12, 2013
Posts: 172


Matthew Brown wrote:As things stand, that won't compile (because you haven't declared the sub variable). Show us some code that will compile and the output you get from it (or errors, if there are any), and we can explain what's going on.

There is no problem in compiling the aforementioned code. It compiles and runs fine. What I'm trying to understand is the logic that undergoes in the above code!! Please refer to the question asked in the original post
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4397
    
    8

Ashish Dutt wrote:
There is no problem in compiling the aforementioned code. It compiles and runs fine. What I'm trying to understand is the logic that undergoes in the above code!! Please refer to the question asked in the original post

Well, yes, now you've posted different code it will compile. You need to be precise about the question you're asking - the missing bits might be important. And you still haven't told us what the output is. I'm asking for clarification because it's not entirely clear to me what the original question was really asking.
Ashish Dutt
Ranch Hand

Joined: Apr 12, 2013
Posts: 172

Matthew Brown wrote:
Ashish Dutt wrote:
There is no problem in compiling the aforementioned code. It compiles and runs fine. What I'm trying to understand is the logic that undergoes in the above code!! Please refer to the question asked in the original post

Well, yes, now you've posted different code it will compile. You need to be precise about the question you're asking - the missing bits might be important. And you still haven't told us what the output is. I'm asking for clarification because it's not entirely clear to me what the original question was really asking.


Okay the rephrased question again is as follows:
Why is it that the reference variable super1 of SuperClassExp not able to access the instance variable of subClassExp even though it has been assigned to it using the assignment operator. as
With the above assignment done The reference variable super1 is only able to access superclVar using the dot operator. I want to know why it cannot access the intstance variable named even though it has been assigned the SubClass.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4397
    
    8

OK, I think I know what you're asking now.

The important think is the reference type. Given any variable there are two types. The reference type depends on how the variable is declared, and this is what the compiler knows about. Then there's the actual type of the object it references. The compiler will only allow operations that it can guarantee will work, so it will only allow access to member that belong to the reference type. If you want to access something in the actual type that isn't part of the reference type, then you need a cast:

When you write:
Here the reference type is superClassExp. It's actually referencing a SubClassExp, but the compiler doesn't know that. So if you want to access the subClassVar instance variable you need a cast:

Does that explain what you were asking about?

(And I'd strongly recommend switching it to SuperClassExp - using inconsistent capitalisation makes the code harder to read).
Ashish Dutt
Ranch Hand

Joined: Apr 12, 2013
Posts: 172

Matthew Brown wrote:OK, I think I know what you're asking now.

The important think is the reference type. Given any variable there are two types. The reference type depends on how the variable is declared, and this is what the compiler knows about. Then there's the actual type of the object it references. The compiler will only allow operations that it can guarantee will work, so it will only allow access to member that belong to the reference type. If you want to access something in the actual type that isn't part of the reference type, then you need a cast:

When you write:
Here the reference type is superClassExp. It's actually referencing a SubClassExp, but the compiler doesn't know that. So if you want to access the subClassVar instance variable you need a cast:

Does that explain what you were asking about?

(And I'd strongly recommend switching it to SuperClassExp - using inconsistent capitalisation makes the code harder to read).

Dear Matthew,
That's what i was asking. Thanks a lot for the clarification that makes more sense.
Also the naming convention, I am aware of it just missed it but thanks for pointing it out. Will take care next.
 
GeeCON Prague 2014
 
subject: Confused with the assignment operator!