dont understand constructor...

S

Sreenivas

frnds ,i have a problem with this program..
i got the output but could anybody tell the reason.
class a{
a(){
System.out.println("Q");
}
}
class b extends a{
b(){
System.out.println("W");
}
}
class c extends b{
c(){
System.out.println("E");
}
}
class output{
public static void main(String [] args){
c Object =new c();
}
}
The output is :
Q
W
E
could anybody tell reason???????????
 
R

RedGrittyBrick

Sreenivas said:
frnds ,i have a problem with this program..
i got the output but could anybody tell the reason.
class a{
a(){
System.out.println("Q");
}
}
class b extends a{
b(){
System.out.println("W");
}
}
class c extends b{
c(){
System.out.println("E");
}
}
class output{
public static void main(String [] args){
c Object =new c();
}
}
The output is :
Q
W
E
could anybody tell reason???????????

Presumably this is homework and you were txting your friends instead of
paying attention in class :)

The Java compiler inserts a call "super()" to the superclass constructor
at the start of a subclass constructor unless you do so explicitly
yourself (with or without arguments).

So C's constructor first calls B's constructor (because C extends B)
which first calls A's constructor (because B extends A.)

Some other notes:
- Indent code to make it easier to read.
- Class names should start with an upper-case letter.
- Variable names should start with a lower-case letter.
- "Object" is a poor name for a variable.
- Instead of printing Q,W,E it would be more logical to print A,B,C.
- One question mark is enough. Your degree of puzzlement does not affect
the answer you get and anyway, there is no internationally calibrated
system for assigning a number of question marks to degree of puzzlement. ;-)

All these would make your question easier to read. I believe making a
question easy to read ought to be a goal, if you want answers.
 
R

rossum

frnds ,i have a problem with this program..
i got the output but could anybody tell the reason.
class a{
a(){
System.out.println("Q");
}
}
Modify your class a to look something like:

class AClass {
private int aVariable;
public AClass() {
System.out.println("Constructing AClass.");
aVariable = 42;
}
public int getAVariable() { return aVariable; }
}

The modified class now has its own member data, and its constructor
has some work to do setting it. There is also a getter to return the
value of the data variable.

class b extends a{
b(){
System.out.println("W");
}
}
Same here:

class BClass extends AClass {
private double bVariable;
public BClass() {
System.out.println("Constructing BClass.");
bVariable = 123.0;
}
public double getBVariable() { return bVariable; }
}

class c extends b{
c(){
System.out.println("E");
}
}
And again:

class CClass extends BClass {
private String cVariable;
public CClass() {
System.out.println("Constructing CClass.");
cVariable = "Hello World!";
}
public String getCVariable() { return cVariable; }
}
class output{
public static void main(String [] args){
c Object =new c();
}
}
Now amend you main() method to look something like:

public static void main(String [] args){
CClass myCObject = new CClass();
System.out.println("myCObject.aVariable = " +
myCObject.getAVariable());
System.out.println("myCObject.bVariable = " +
myCObject.getBVariable());
System.out.println("myCObject.cVariable = " +
myCObject.getCVariable());
}

The output is :
Q
W
E
What do you expect the output to be from your modified program? What
values do you expect for myCObject.aVariable, myCObject.bVariable and
myCObject.cVariable? For each of those three values say where it was
set.
could anybody tell reason?
Now you should be able to tell yourself the reason.

rossum
 
S

Stefan Ram

Sreenivas said:
The output is :

The sequence required by the language specification is (simplified):

0 set fields to default values as they are specified by
the language

1 activate the constructor, which includes the steps

1.0 activate the (implicit or constructor-called)
constructor of the superclass

1.1 do the explicit initializations (variable and instance
initializers) in the source-code order

1.2 execute the rest of the constructor

for example:

class B
{ int a = Do.print( "B0" );
{ Do.print( "B1" ); }
int b = Do.print( "B2" );
public B(){ Do.print( "B4" ); }
{ Do.print( "B3" ); }}

class A extends B
{ int c = Do.print( "A0" );
{ Do.print( "A1" ); }
int d = Do.print( "A2" );
public A(){ Do.print( "A4" ); }
{ Do.print( "A3" ); }}

public class Main
{
public static void main( final java.lang.String[] args )
{ new A(); }}

class Do
{ public static int print( final java.lang.String text )
{ java.lang.System.err.println( text ); return 1; }}

/* prints
B0
B1
B2
B3
B4
A0
A1
A2
A3
A4
*/
 
D

Daniel Pitts

Stefan said:
class B
{ int a = Do.print( "B0" );
{ Do.print( "B1" ); }
int b = Do.print( "B2" );
public B(){ Do.print( "B4" ); }
{ Do.print( "B3" ); }}
Are you by any chance a LISP programmer? You often post code that makes
me think so.
 
L

Lew

Roedy said:
inside a constructor, if you don't explicitly call the superclass
constructor with super, first thing, Java inserts one for you.

See http://mindprod.com/jgloss/constructor.html

The idea is you have to invoke all the constructors to get all the
fields initialised.

Herein is one of the things about constructors that makes them different from
methods.

It is important to remember that constructors are different from methods.

Constructors' primary purpose is the manufacture of instances. Methods can
assume they live in completely constructed instances; constructors must not.

Weird things happen when you call methods from constructors without
remembering these differences, especially if the methods are or can be overridden.

The safest is to focus constructors on construction. Don't put anything or
call anything from a constructor that isn't intrinsically part of building the
object. This can be a fair bit of work - DAO objects legitimately will open
connections as part of construction, for example. But it's work for one
purpose - to build the object.

Other work, so-called "behaviors", must not happen in a constructor.

Remembering the difference between constructor and method, you create
unassailable classes.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top