• 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
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Help me to get the output.............I Know the answer but tell me how you got that?

 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class A{
int a=10;
A()
{call();}
void call(){
System.out.println("a="+a);
}
}
class B extends A
{
int b=16;
B(){
call();
}
void call()
{
System.out.println("b="+b);
}
public static void main(String args[])
{
B ob = new B();
}
}
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Manoj,



Output:
b=0
b=16

Facts to think about:
1- If method is overridden, which method to call at run time
2- Order of initialization
3- Constructor call
4- Until constructor of the the parent class completes, subclass member variables are not initialized.
5- Sub class object is created, from the parent class constructor you make call to the method that is overridden in the subclass, subclass version is selected at run time.
6- When parent class constructor runs value of "b" gets its default (0 for integer member variable) and, after that b gets its value 16 before the constructor of the subclass executes.


Think, you will get the answer

Thanks and Regards,
cmbhatt
[ April 07, 2007: Message edited by: Chandra Bhatt ]
 
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
oops how its print b=0 ??

i thought super constructor will call its method , but it doesn't

even if he call sub method how its become 0 an we assign 16 to it ?
 
Ranch Hand
Posts: 893
Tomcat Server Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The super constructer within class A will call it's method, but because of inheritence it will call the method of class B which will print the value of b within class B,

Because the constructor of class B has not run the default value will be displayed and this is 0.

The following code will make this clear

 
Manoj Mani
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good answer..................Keep replying to my doubts....Have a nice day..
 
Popeye has his spinach. I have this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic