Win a copy of Succeeding with AI this week in the Artificial Intelligence and Machine Learning forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Junilu Lacar
Sheriffs:
  • Tim Cooke
  • Jeanne Boyarsky
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • fred rosenberger
  • Frits Walraven

whats happening here!!

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all
consider the following code:

please explain me with whats happening
Noel
[This message has been edited by Noel Hinders (edited March 09, 2001).]
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Noel,
I think u 've misquoted your question, coz what i see in your code is nothing is tricky and class in t2 is not getting envolved in any way.
Only thing I see tricky in this case is that all of three classes are declared in a single file, since all three classes are declared public, and a file can't can 've more than 1 public class or interface.
if that's not the case than it should print==>
2
2
'coz u r creating object of t1 in the main of t.
as there is no constructor in t1 , i is getting intialized to 2.
so call to tins.i prints 2
and call to tins.amethod prints 2.
I hope this helps u.
bye
ashish
 
Noel Hinders
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ashish,
I have edited the code
t1 tins= new t2();
please consider the example now.
thank you.
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
If these three classes are created in three separate files, however, the output after you compile and run the t class will be
2
3
I believe this has something to do with either dynamic binding or the subclass t2 hiding t1's amethod. I'm also a bit confused as to why this outputs 3 for tins.amethod call. http://members.spree.com/education/javachina/Cert/FAQ_SCJP3.htm#inh_Q4
may have some useful info, but I'm not sure I understand if it relates to this since this one does not have static methods. My guess is that t2's amethod hides t1's amethod.
Can someone please clarify for us on this? Much thanks.
 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David,
You are right. When you call a method, java sees the class of the object this reference is referencing to. In this case its t2. Thats why it calls method from t2 class and hence i = 3. (Dynamic method binding)
Whereas when you ask for a value of a variable, Java sees the class of the reference and hence value for i is fetched from t1 class and in this case it is equal to 2.
hope this helps.
Thanks
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
When you reference instance variables, the declared type of the object reference is used. In the example, the declared type of the object reference variable <code>tins</code> is <code>t1</code> so '2' is printed.
When you call methods, the runtime type is used. In the example, the runtime type is <code>t2</code>. <code>amethod()</code> in class <code>t2</code> is called, and as there is an instance variable <code>i</code> in <code>t2</code>, '3' is printed.
See JLS §15.11 Field Expressions
Hope that helps.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
 
David Basile
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much, Jane and everyone on this question!
I've seen the references to the JLS in other postings on this site, but never referred to it(boy, do I feel foolish!). The JLS seems very thorough in its explanations and serves as a good complement to the books I've been reading and these types of forums.
 
Noel Hinders
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
thanks for the info,
I think I should refer to JLS.
I have got it now,
how important is it to gothru jls
any comments???
Noel
 
Jane Griscti
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Noel,
IMHO the JLS is a must read; the language is a bit of a pain but it definitely helps to work through the example code it contains.
Hope that helps.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
 
This cake looks terrible, but it tastes great! Now take a bite out of this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic