• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

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..
 
You would be much easier to understand if you took that bucket off of your head. And that goes for the tiny ad too!
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic