Win a copy of OCP Oracle Certified Professional Java SE 11 Programmer I Study Guide: Exam 1Z0-815 this week in the Programmer Certification forum!
  • 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
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Paweł Baczyński
  • Piet Souris
  • Vijitha Kumara

constructor initialize order, flow of program please

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thx for u r reply
but i want to know how the below code runs.
basically the step by step flow of the program.
which constructor is called ,
the output below is correct but i cant yet understand the
flow of the code below .
can anyone xplain to me the flow of the code below.
thx
kamal j

class superclassa
{
protected int supervalue;
protected int i=10;
public superclassa()
{
System.out.println("constructor in superclass");
this.dovalue();
System.out.println(this.i);
}
public void dovalue()
{
this.supervalue=911;
System.out.println("supervalue:"+this.supervalue);
}
}
class subclassb extends superclassa
{
private int value=800;
public subclassb()
{
System.out.println("constructor in subclassb");
i=30;
this.dovalue();
System.out.println("supervalue:"+this.supervalue);
}

public void dovalue()
{
System.out.println("value:"+this.value);
}
}
public class object1
{
public static void main(String[]args)
{
subclassb b=new subclassb();
superclassa a=new superclassa();
System.out.println(b.i);
}
}
OUTPUT BELOW:::
constructor in superclass
value:0
10
constructor in subclassb
value:800
supervalue:0
constructor in superclass//line number 49.
supervalue:911
10
30
 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
subclassb b=new subclassb();
Whenever this statement is executed,
First the superclass constructor of this object is called.
That's why you get the following output from superclassa() constructor

constructor in superclass -> First statement in superclassa() constructor
value:0 -> This is the most confusing part of this program
when this.dovalue() function is called in superclassa() since its an overriden function(dynamic polymorphism) dovalue() of subclassb will be called.when dovalue of subclass is called and since the subclass object hasnot been created yet, value of 'value' variable will be zero. That's why u get the output for this statement as 0

10 -> prints the value of i in superclassa

Now the constructor of this object will be called. So the output
constructor in subclassb -> First statement in subclassb() constructor
value:800 -> since the object has been created (i.e. after calling the superclassa constructor and initializing the data members of this class) this shows the right value
supervalue:0 -> This is inherited from superclassa and we hvn't initialized this variable with anyvalue so far for this object so the value is 0
The following output is because of this statement in main()
superclassa a=new superclassa();
constructor in superclass -> first statement in superclassa constructor is called
supervalue:911 -> it executes the dovalue() of superclassa
10 -> this is the value of i in superclassa

30 -> this is the output of this statement System.out.println(b.i);

I think this will help
cheers
Siva
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
General rules for object construction (in the order of implementation) may be summarised as follows:
1. Static variables in the super class are initialised.
2. Static variables in the child class are initialised.
3. Instance variables in the super class are initialised.
4. Super class constructor(s) are executed.
5. Instance variables in the child class are initialised.
6. Child class constructors are executed.
Note - Static variable initialisation is done only once, when the class is loaded. Instance variable initialisation is done each time an object is created.
You may work your way out with the help of above rules - it is easy. Since there are no static variables, lines 1 & 2 do not apply in this particular case.
 
These are not the droids you are looking for. Perhaps I can interest you in a tiny ad?
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!