• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Confused with the assignment operator!

 
Ashish Dutt
Ranch Hand
Posts: 172
Java MySQL Database Python
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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.
 
Matthew Brown
Bartender
Posts: 4549
8
Java Netbeans IDE Scala
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 172
Java MySQL Database Python
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 4549
8
Java Netbeans IDE Scala
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 172
Java MySQL Database Python
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4549
8
Java Netbeans IDE Scala
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 172
Java MySQL Database Python
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic