Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

overriding vs hiding

 
steven gerrard
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
wats the difference between hiding and overriding
can some pls explain
 
Mani Ram
Ranch Hand
Posts: 1140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check JavaRanch FAQ
 
steven gerrard
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • 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 ]
 
Chris Allen
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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:
 
Anju sethi
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for good explanation
 
Maxwell Oyom
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
that was a wonderful brakdown of explanation. U surely know how to teach the technology.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic