• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

constructors  RSS feed

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • 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 Test {
public static void main(String[] args){
Base b = new Derived();
System.out.println(b.getValue());
}
}

The output is 40. Can anyone help me in understanding how 40 is the output?
 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The derived addValue() method is called twice. Once from the base constructor, and once from the Derived constructor. Since the runtime type of b is a Derived, the addValue method that is called from is is always the overridden one in the Derived class. I added some println() statements in your code to illustrate.
 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

Can someone care to explain to me what does this statement imply..
Base b = new Derived();
I am trying to derive the flow and am uncertain as to why it moves abt all over the place...
I am brushing up on the basics of the language...so do be patient if my qn is blunt....

thanks..
 
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Shafian Kisna:
hi,

Can someone care to explain to me what does this statement imply..
Base b = new Derived();
I am trying to derive the flow and am uncertain as to why it moves abt all over the place...
I am brushing up on the basics of the language...so do be patient if my qn is blunt....

thanks..



Creates an instance of the class Base and instantiates it with the null constructor from Derived.
 
author
Posts: 23832
140
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Shafian Kisna:
hi,

Can someone care to explain to me what does this statement imply..
Base b = new Derived();



This instruction declares a reference variable "b", which is assigned to a Derived object instantiated using the constructor that takes no parameters. This assignment works because the Derived object is-a Base object.

Henry
 
And when my army is complete, I will rule the world! But, for now, I'm going to be happy with this tiny ad:
ScroogeXHTML - small and flexible RTF to HTML converter library
https://coderanch.com/t/710903/ScroogeXHTML-RTF-HTML-XHTML-converter
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!