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.
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.
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.
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.
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.
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.