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


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "why the output is so?" Watch "why the output is so?" New topic
Author

why the output is so?

dolly shah
Ranch Hand

Joined: Jun 18, 2007
Posts: 383
class Base{
int value = 0;
Base(){

addValue();
}
void addValue(){
value += 10;
}
int getValue(){

return value;
}
}
class Derived extends Base{
Derived(){
addValue();

}
void addValue(){
value += 20;
}
} public class Test2 {
public static void main(String[] args){
Base b=new Derived();
System.out.println(b.getValue());

How the output is 40?
i ran it I got 40. But I am not getting how it is 40? After very first line of main method, code goes in Derived constructor & call super() (by JVM), code goed to Base constructor. Increase value=10. Then code come back to Derived constructor & increase value=10+20=30. Then how it is 40?

Can anyone explain?


SCJP-1.5<br />SCWCD-1.4
christian combarel
Ranch Hand

Joined: Aug 04, 2007
Posts: 47
As the addValue() method is overiden in Derived class, this is THAT version that is used and not the one from the superclass, even when you construct the Base part of the Derived class.
In short, when you instanciate the Derived class addValue() from that class is called twice and addValue() from Base is never called.



-------------
Chris
dolly shah
Ranch Hand

Joined: Jun 18, 2007
Posts: 383
Why it is like that? Can you explain little more, what is the concept in behind of that?
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

See How my dog learned polymorphism.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
dhwani mathur
Ranch Hand

Joined: May 08, 2007
Posts: 621
Hi All


Please can anyone explain the
above shown program in detail

to How the output is 40?



Till now what i understood is as below





In the main method the super class
constructor is called ie Base()
which consist of addValue method

now here initialy value was =0
so it becomes value+=10
-----value=10-------

now Derived class constructor is called
Derived() which consist of overriden method
addvalue
so value was=10
so now it becomes
value+=20
10+20=30
-------value=30-----------

later the b.getvalue() method
is called which returns the value

so value =30


i dont know where i am going wrong
please if anyone could correct me
it will be kinda of you

Thanks in advance


Preparing SCJP 1.5
dolly shah
Ranch Hand

Joined: Jun 18, 2007
Posts: 383
Thanks marc I got it little bit. But one question raise in my mind. After Base b=new Derived();call, code goes to Base's constructor, Base's constructor calls addValue() method, my question is at this point Derived class's addValue() method calls or Base class's?
Gitesh Ramchandani
Ranch Hand

Joined: Feb 28, 2007
Posts: 274
Originally posted by dolly shah:
Thanks marc I got it little bit. But one question raise in my mind. After Base b=new Derived();call, code goes to Base's constructor, Base's constructor calls addValue() method, my question is at this point Derived class's addValue() method calls or Base class's?



After Base b=new Derived();call, code goes to Base's constructor, then as the runtime object is Derived type it calls Derived class's addValue() method, then returns back to derived class constructor and again Derived class's addValue() method is called, making value-40.

Hope I'm clear,

Gitesh
dolly shah
Ranch Hand

Joined: Jun 18, 2007
Posts: 383
Thanks everybody.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: why the output is so?
 
Similar Threads
What will this program print out? Why?
constructors
Overriding
confusion in the output
polymorphism Problem (tricky)