• 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
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

overriding vs hiding

 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi
wats the difference between hiding and overriding
can some pls explain
 
Ranch Hand
Posts: 1140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check JavaRanch FAQ
 
steven gerrard
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thnks a lot

kindly explain this strange output::
class b{int i=15;
public b(){
add();
}
public void add(){
System.out.println(2*i);
}
}
public class A extends b {int i =7;
public A(){
add();
}
public void add(){
System.out.println("" + (2*i));
}
public static void main(String[] args) {

A a = new A();
}

}

the output of this program is:
0
14

where does this 0 comes in output
kindly explain
[ February 07, 2006: Message edited by: steven gerrard ]
 
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I had to add some println statements to figure this one myself but basically, it boils down to the order in which the objects are constructed and the completion of the constructors. When the new A object is constructed, the first thing it does is run the b constructor (since there is an implicit call to the super class constructor). Within b's constructor, it calls the add() method. The key here is that it calls A's add method, not B's method (since it is an A object, it will call A's add() method). At this point, the A constructor has not completed so the value of i in the A class has not been initialized. Only once A's constructor is completed will the value of A's instance variable i be initialized to 7. The execution continues with the call to A's add() method and you get the value of 2*i, which is 14.
 
steven gerrard
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Chris Allen:
At this point, the A constructor has not completed so the value of i in the A class has not been initialized. Only once A's constructor is completed will the value of A's instance variable i be initialized to 7. The execution continues with the call to A's add() method and you get the value of 2*i, which is 14.



SO at wat stage does 'i' in class A initialized
first of all in A s constructor super() is called

then add is called in b s constructor . but since i in A is not initalized as yet hence 0 is printed

now again add is invoked ,thi stime in A s constructor

so when did i got initialized???
 
Chris Allen
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oops , let me correct one statement:


Only once A's constructor is completed will the value of A's instance variable i be initialized to 7.


This should be b's constructor is completed will the value of A's instance variable i be initialized to 7.

Here is the modified code I used which should demonstrate this:
 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for good explanation
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
that was a wonderful brakdown of explanation. U surely know how to teach the technology.
 
You would be much easier to understand if you took that bucket off of your head. And that goes for the tiny ad too!
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic