• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Rob Spoor
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Holloway
  • Piet Souris
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Himai Minh

Inheritance

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Everyone,
I am very confused with this program. I am a beginner so please help me with this.


I want to understand that we are calling the m1() method using job class object and we have also declared int i again in job class, then why it is still printing the int i variable of test class. I am confused with this, please help me.
Thank you.
 
Marshal
Posts: 73766
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Where is this example from?

Don't say “class object” because there is such a thing as a “Class object” which you are at risk of confusing it with. Say, “object”.
Fields are not polymorphic, and you have two bits of bad practice in that code. One is that you are hiding a field by redeclaring it in a subclass. The other is that you have non‑private variables. Because fields are not polymorphic, the choice of field is bound at compile time according to the declared type of the reference, which is job. But the m1() method is unaware of the existence of the subtype, so it cannot bind to 5. Please always use CapitalLetters for ClassNames.

Moving discussion to our OCA forum.
 
sahil Kairon
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Really sorry for confusing you. I mean to say Class job Object and Class test Object and I am confused that why it is giving 4 as an output and not 5.
We have redeclared int i variable in Class job and we are calling m1() method with class job Object.
 
sahil Kairon
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

 But the m1() method is unaware of the existence of the subtype, so it cannot bind to 5. Please always use CapitalLetters for ClassNames.

Moving discussion to our OCA forum.


And I guess I got my answer. Thank you so much. I think this is the reason why output is 4 in above program.
 
Campbell Ritchie
Marshal
Posts: 73766
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sahil Kairon wrote:. . . . I mean to say Class job Object and Class test Object . . . .

No, you didn't. Please don't go and add capital letters; that is simply adding to the error.
Call it an object or a Job object and leave it at that.

We use lots of jargon, and jargon is very precise and informative, but only if it is used correctly.
 
sahil Kairon
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well i will not be using capital C in class while writing it in code, I just wrote it for highlighting it. What you are saying is also true, will take care of it in future. Thanks for explaining things.
 
Campbell Ritchie
Marshal
Posts: 73766
332
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You might use the Class class; beginners most commonly use it in the expression xyz.getClass().getName().
Capital C: name of a class. Small c: a keyword. You need to get used to Java┬ž's case sensitivity.
 
sahil Kairon
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well that's true,  java is strictly case sensitive language and it takes quite practice and time for becoming used to it.  Thanks Campbell for all your help, it was really nice discussing on this topic with you. Keep up the good work.
 
Campbell Ritchie
Marshal
Posts: 73766
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sahil Kairon wrote:. . .  Thanks . . . Keep up the good work.

That's a pleasure
 
Time is mother nature's way of keeping everything from happening at once. And this is a tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic