aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes overriding vs hiding Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "overriding vs hiding" Watch "overriding vs hiding" New topic
Author

overriding vs hiding

steven gerrard
Ranch Hand

Joined: Jan 21, 2006
Posts: 55
hi
wats the difference between hiding and overriding
can some pls explain
Mani Ram
Ranch Hand

Joined: Mar 11, 2002
Posts: 1140
Check JavaRanch FAQ


Mani
Quaerendo Invenietis
steven gerrard
Ranch Hand

Joined: Jan 21, 2006
Posts: 55
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

Joined: Feb 01, 2003
Posts: 127
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

Joined: Jan 21, 2006
Posts: 55
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

Joined: Feb 01, 2003
Posts: 127
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

Joined: Dec 26, 2005
Posts: 91
thanks for good explanation


thanks,<br />Anju Sethi
Maxwell Oyom
Greenhorn

Joined: Jan 18, 2006
Posts: 10
that was a wonderful brakdown of explanation. U surely know how to teach the technology.


Divine 4 eva
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: overriding vs hiding