• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

need help on 2 queries

 
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
please explain why the output is as given
1.The output for the below given code is 3. It could have easily been 1 also. How is the method choosen here?

2.the output is
0
parent

acc to my knowledge,the constructor is called after static and instance initialisation then why i is given value 0 instead of 30???
[Edited to fix formatting - Thomas Paul]
[This message has been edited by Thomas Paul (edited April 19, 2001).]
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Answer1:
The default type for integral literals is Integer. So when you pass 3 , it is considered as integer. This is the reason the value is converted to double type(wideneing conversion).

Answer 2:
The reason for the value 0 is simple. First the base class object is created by calling the instance initializer and then the constructor.In the constructor you are calling the overridden method print(). This will call the subclass's print method. In this method you are trying to access the subclass's instance variable i,which is not yet initialized.

I hope you understand the answer.

I would like to know how can I change my username.

Regards,
Pasupathi J
 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi preeti ,
1. Nop.only the output will be 3 . Thats all. The value u r giving in the method parameter must be cast-compatiable with method parameter type.Only in method three all r converted to double.(wideneing )
2.
Some little bit change to ur code.
class A
{
int i=30;
A()
{
print();
}
void print()
{System.out.println(i);}
void method1(){
System.out.println("Parent");
}
}
public class test extends A
{
int i=Math.round(3.5f);
void print()
{
//line 1
System.out.println(i);
}
void method2(){
System.out.println("Child");
}
public static void main(String args[])
{
test a=new test();
a.method1();
a.print();
}
}
now .the o/p will be 0 parent 4 .
Yes..u r right that static and instance expression called b4 constructor BUT they at first intialized or get the default values.
Not the exact value they assigned. As u calling 4rm the constructor they r showing there default values.
Hope that helps.
BUt ...my q. 2 others,if I add some code like 'super.print()' ..then the o/p will be 30 0 parent 30 4 . But how ??
Thanks in Advance.
<marquee> Ratul Banerjee </marquee>
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ratul,
About the Question 2, I believe you mean adding the line
super.print()
to the print method of the Class test.
This creates an output of 30 0 Parent 30 4.
If that is what you mean, then this can be explained by the shadowing behaviour of the instance variables. When the variable i is accessed in the Print method of Class A, then variable i of Class A shadows the variable i of Class test and the opposite happens when accessing i from Class test.
Thats my understanding. Hope this helps.
-Basant.
[This message has been edited by Basant Sahu (edited April 19, 2001).]
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Pasupathi J,
To change your user name, just re-register using a name that complies with the JavaRanch Name Policy and use that name when you visit.
Hope that helps.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic