• 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

why the output is so?

 
Ranch Hand
Posts: 383
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
dolly shah
Ranch Hand
Posts: 383
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why it is like that? Can you explain little more, what is the concept in behind of that?
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
See How my dog learned polymorphism.
 
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 383
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 383
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks everybody.
 
permaculture is a more symbiotic relationship with nature so I can be even lazier. Read tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic