• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Constructors

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
Can any body clarify the doubts.....
class Bindu{
public String s="DevJani";
public Bindu(){
this(1);
this.method();
System.out.println(this.getClass());
System.out.println("In Bindu");
System.out.println(s);
}
public Bindu(int x){
this.method();
System.out.println("In Bindu(x)");
}
public void method(){
System.out.println("Bindu Method");
System.out.println(s);
}
public static void main(String args[]){
Suresh s=new Suresh();
}
}
class Suresh extends Bindu{
String s="Babu";
public Suresh(){
System.out.println("In Suresh");
System.out.println(s);
}
public Suresh(int x){
System.out.println("In Suresh(x)");
}
public void method(){
System.out.println(" Suresh Method");
System.out.println(s);
}
}
OutPut is:
Suresh Method
null
In Bindu(x)
Suresh Method
null
class Suresh
In Bindu
DevJani
In Suresh
Babu
****************.
Please clarify the follwoing doubts...
In Bindu() constructor..
this.method() .. this referes to Current OBject Here it is Suresh.. It is calling
Suresh's method
1. But when you use this(1).. according to above point it should call Suresh(1),
but it is calling Bindu(1).. why?
2. Why Out put is Null in Subclass method..
thanks in advance..
suresh
[This message has been edited by suresh seeram (edited October 06, 2001).]
 
Ranch Hand
Posts: 2596
Android Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Note that, whenever you construct an object of derived class,
it must complete constructor call(or calls) of the base class(or classes)
from top to bottom.
Here is what is happenning -
(1)In main you call default non parameterized constructor of Suresh.
(2) Suresh extends Bindu, hence Bindu's corresponding non-parameterized constructor is called.
(2) In Bindu(), you call constructor Bindu(int x) with value 1 (via this(1), constructors are not overridden), which in turn calls method "method()",
but method() is a class method/function and is overridden in Suresh, hence call is resolved to Suresh.method() at runtime and first line printed is - " Suresh Mehod". The next statement in method() of suresh is
supposed to print string s -- "Babu", which is an instance variable, it does not exist at this moment because the object of Suresh is not yet instantiated(can not be created w/o completing its Base class constructors). Hence prints null.
(3) Next, after resolving polymorphic "method()" call,the control returns to next executable statement of Base(int x), from where "method()" was called. This prints "In Bindu(x)" on next line.
(4) Again, control returns to Base(), from where Base(1) was called, again this.method() call gets resolved to its polymorphic derived class implementation and prints " Suresh Method". Agin same explanation (as above) applies for "System.out.println(s);" call in Suresh.method(), hence prints null.
(5) Next executable statement in Base() (we haven't finished it yet!!!), is -
"System.out.println(this.getClass()); ", which *does* refer to the actual class
which is loaded by JVM - hence prints "class Suresh.
(6)Next is straight forward, prints - "In Bindu", next statement prints Bindu's own instance variable s, which is DevJani. Member variables *do not* display any virtual, polymorphic behaviour.
Yes! Done with base class. So far so good.
(7) Now, constructor of Suresh, first executable statement is "System.out.println("In Suresh");", prints - "In Suresh, next statement is - "System.out.println(s);", which refers to instance variable s, having value "Babu". The value is printed. Note that, this time value is printed because call to the constructor of base class has been completed and hence instance variables do exist.
I hope this makes it clear. Answers to your specific questions -


Please clarify the follwoing doubts...
In Bindu() constructor..
this.method() .. this referes to Current OBject Here it is Suresh.. It is calling
Suresh's method
1. But when you use this(1).. according to above point it should call Suresh(1),
but it is calling Bindu(1).. why?

Constructors are diffrent from methods and are neither inherited, nor do they display any virtual, polymorphic behaviour. Hence, here a call in base class Bindu() gets resolved to its own parametrized constructor.

2. Why Out put is Null in Subclass method..

Because, at that time, the call to the base class constructor is not complete, hence derived class object, and its instance variables do not exist.


HTH,
- Manish
[This message has been edited by Manish Hatwalne (edited October 06, 2001).]
 
reply
    Bookmark Topic Watch Topic
  • New Topic