• Post Reply Bookmark Topic Watch Topic
  • New Topic

is there a concept on overriding with instance variables?  RSS feed

 
Shibu Kurian
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why am i getting superclass default value?


Why is this giving me 10?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Instance variables are not overridden. The compiler uses the reference type to determine which instance variable to access.

And BTW, in your code example, I am assuming that the second print with the obj2 reference.


Henry
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

That shows how much confusion you can create for yourself if you try giving a subclass fields with the same name as the superclass.
 
Shibu Kurian
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:Instance variables are not overridden. The compiler uses the reference type to determine which instance variable to access.

And BTW, in your code example, I am assuming that the second print with the obj2 reference.


Henry


yeah its obj2...sry for the typo.
I got that inference from the output. But the same doesn't happen for methods of the class. That's the reason why i wanted to know whether this is allowed for any particular situations/contexts in real life.
 
Shibu Kurian
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Welcome to the Ranch

That shows how much confusion you can create for yourself if you try giving a subclass fields with the same name as the superclass.


we do the same thing for overriding methods!!!...lets look at this as an R&D question....more of which can be expected in SCJP questions too. We don't respond to exam questions like this right?
I'm trying to learn here...not to confuse or get confused.
 
praveen kumaar
Ranch Hand
Posts: 461
22
Android Chrome Eclipse IDE Google App Engine Java Notepad Oracle Ubuntu Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shibu Kurian wrote:we do the same thing for overriding methods!!!...lets look at this as an R&D question....more of which can be expected in SCJP questions too. We don't respond to exam questions like this right?
I'm trying to learn here...not to confuse or get confused.
Hi shibhu,campbell uses the term confusion to make you learn a good habit while coding,don't use the duplicate name in your subclass field accordingly with the super class,it can jumbled the thing's in your(along with instructor's and a guide) mind.so here he makes you to learn 2 things at a time-"solution to your doubt+his suggestion".
so please don't talk like you did in your previous post,instead you should apologise for it.

Kind Regards,
Praveen.
 
salvin francis
Bartender
Posts: 1664
37
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shibu Kurian wrote:...
I got that inference from the output. But the same doesn't happen for methods of the class...


Which is true, methods are overriden, that what polymorphism is all about.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shibu Kurian wrote:
Campbell Ritchie wrote:That shows how much confusion you can create for yourself if you try giving a subclass fields with the same name as the superclass.

We don't respond to exam questions like this right?
I'm trying to learn here...not to confuse or get confused.

You misunderstood what Campbell was saying; there was no malice intended in his comment.

All Campbell meant was that it is not a good idea to use a name in a subclass if the same name is already used in one of its superclasses because it can result in unintended or unexpected behavior and that can cause some confusion, just as it did with you. You are confused as to why you are seeing behavior you did not expect, are you not? Even experienced developers who understand the rules behind this will have to look at the code carefully to understand what's going on. In other words, it can confuse even experienced developers like those who are offering clarifications to you on this thread. You shouldn't take statements like that too personally. We know you're trying to learn; were only trying to help, too.
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry for causing confusion; Praveen Kumaar and Junilu Lacar explained it so much better than I did; thank you
 
Shibu Kurian
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
praveen kumaar wrote:
Shibu Kurian wrote:we do the same thing for overriding methods!!!...lets look at this as an R&D question....more of which can be expected in SCJP questions too. We don't respond to exam questions like this right?
I'm trying to learn here...not to confuse or get confused.
Hi shibhu,campbell uses the term confusion to make you learn a good habit while coding,don't use the duplicate name in your subclass field accordingly with the super class,it can jumbled the thing's in your(along with instructor's and a guide) mind.so here he makes you to learn 2 things at a time-"solution to your doubt+his suggestion".
so please don't talk like you did in your previous post,instead you should apologise for it.

Kind Regards,
Praveen.


Yes Praveen. I do know that this is not a good practice. I did mention to take this as an R&D question, not a question of a real life context. And yes, i wanted to ensure that the context of the question is clear enough.
This question is particularly to know whether Java has an intention of allowing this to happen and any particular reason on why the base class value is being printed rather than the derived class just like how it does for the method overriding feature.

 
Shibu Kurian
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Sorry for causing confusion; Praveen Kumaar and Junilu Lacar explained it so much better than I did; thank you


Apologies if my statement hurted you or anyone.... seriously i did not take it personally nor intended to hurt anyone. I do take answers technically and theoretically, not personally. It looked like you understood this question as my code used in real life, but mine was just a R&D question like how it normally comes up in a exam or an interview.
Hope this helps.
 
Shibu Kurian
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:
Shibu Kurian wrote:
Campbell Ritchie wrote:That shows how much confusion you can create for yourself if you try giving a subclass fields with the same name as the superclass.

We don't respond to exam questions like this right?
I'm trying to learn here...not to confuse or get confused.

You misunderstood what Campbell was saying; there was no malice intended in his comment.

All Campbell meant was that it is not a good idea to use a name in a subclass if the same name is already used in one of its superclasses because it can result in unintended or unexpected behavior and that can cause some confusion, just as it did with you. You are confused as to why you are seeing behavior you did not expect, are you not? Even experienced developers who understand the rules behind this will have to look at the code carefully to understand what's going on. In other words, it can confuse even experienced developers like those who are offering clarifications to you on this thread. You shouldn't take statements like that too personally. We know you're trying to learn; were only trying to help, too.


I do get the jist of what happens if a variable of the same name is used in the subclass. I also understand that this is not a good practice sir. Let me put it this way, what would be the theory behind the logic, when Java gave  preference to superclass variable than sublass variable, but its the other way round for methods.

Hope you got my question.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Technically, you have to understand how compilers work and the difference between early/static and late/dynamic binding and how that relates to the mechanisms involved to support polymorphism.  I think someone mentioned this before, only methods participate in polymorphism; they are resolved at runtime through the process of late/dynamic binding. Variable members are not polymorphic so they are resolved through early/static binding at compile time.
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shibu Kurian wrote:. . . what would be the theory behind the logic, . . .
It is a design feature of the language. As Junilu says, polymorphism applies only to instance methods.Now, you are passing 123 to the Subclass constructor and that sets i 123. What do you think will be printed? It will start with Superclass or Subclass.
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shibu Kurian wrote:. . . Apologies . . .
Apologies accepted

Sorry for my delay in noticing this post.
 
Shibu Kurian
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:. . . It is a design feature of the language. As Junilu says, polymorphism applies only to instance methods. . . . Now, you are passing 123 to the Subclass constructor and that sets i 123. What do you think will be printed? It will start with Superclass or Subclass.


Correct me if i'm wrong
Point 1
Order of implementation of constructors will always be from superclass to subclass. Having that said...
When 123 is passed to the Subclass constructor, Superclass constructor gets called manually and the value of i(subclass copy) gets the value 123 and then the same gets replaced with 999 as per statement this.i = 999(again subclass copy of i);
Hence the output would be 999. This is fine and expected. Theory matches the practical.

Point 2
Let me make a small change in the main method...
Superclass sc = new Subclass(123);
        System.out.println(sc.i); //123
and
Subclass sc = new Subclass(123);
        System.out.println(sc.i); //999
This is different from your code sir
Superclass sc = new Subclass(123);
System.out.println(sc);//will give 999 due to toString() getting called with Subclass object
Subclass sc = new Subclass(123);
System.out.println(sc);//will give 999 due to toString() getting called with Subclass object

Now is this part of early/late binding sir?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shibu Kurian wrote:
Now is this part of early/late binding sir?


It's actually very easy to remember. Instance methods of the subclass that override the super class (ie. instance method, same method name, same signature, and method of superclass visible to the subclass) are polymorphic, and hence, has a runtime component to resolve/bind the method.

Everything else is resolved at compile time only.

Henry
 
Liutauras Vilda
Sheriff
Posts: 4928
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shibu Kurian wrote:We don't respond to exam questions like this right?
I'm trying to learn here...not to confuse or get confused.
Note, this is a Beginning Java forum, and people who respond have little clue where you took this code snippet from and for what purposes. Usually when you getting ready for a certification exam, I think is a good/better practice to post question to our certification forum, where such questions are expected, so people will know what to expect. Check certification forums category.

Other than that, expect to get advices how not to write code in a bad way. We don't want to confuse newcomers by providing poor formatting, indentation and variables naming

Agh, good luck on exam.
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shibu Kurian wrote:. . . When 123 is passed to the Subclass constructor, Superclass constructor gets called manually and the value of i(subclass copy) gets the value 123 and then the same gets replaced with 999 as per statement this.i = 999 . . .
Hence the output would be 999. . . .
No, that is incorrect. The value 123 is never passed to the Subclass' i field. It is changed to 999 from its default value of 0. The 123 goes to the Superclass' field.

When you said constructors go from superclass to subclass, please explain exactly what you mean, and which order the lines in each constructor are executed in. I am not sure you have got that right.
Point 2
Let me make a small change in the main method...
Superclass sc = new Subclass(123);
        System.out.println(sc.i); //123
No, that code fails to compile because i has private access. If you take away the private modifier, you get 123.
. . .
Subclass sc = new Subclass(123);
        System.out.println(sc.i); //999
This is different from your code sir
Yes, I know that is different.
. . . Now is this part of early/late binding sir?
It is probably better to say compile‑time binding that early binding. It is probably better to say runtime binding that late binding. Henry has already answered that. Instance methods use runtime binding and everything else uses compile‑time binding.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!