• 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
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Jj Roberts
  • Carey Brown
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

probleme in constructor(Help)

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
//Code:Sup
public class Sup
{
protected int x,y;
//no argument constructor
public Sup()
{
x=0;
y=0;
}
public Sup(int a,int b)
{
x=a;
y=b;
//implicit calling toString()
System.out.println(this);
}
public String toString()
{
return "["+x+","+y+"]";
}
}
_______________________________________________________________________
//Code:Sub
public class Sub extends Sup
{
protected double radius;
//no argument constructor
public Sub()
{
//implicit call Super construtor
radius=0;
System.out.println(this);
}
public Sub(double r,int a,int b)
{
super(a,b);
radius=r;
//implicit calling toString()
System.out.println(this);
}
public String toString()
{
return "["+super.toString()+"+"+radius+"]";
}
}
___________________________________________________________________________
//Code:Test
public class Test
{
public static void main(String arg[])
{
Sub b=new Sub(4.5,64,32);
Sub c=new Sub(10,5,5);
}
}
And the output is:[[64,32]+0.0]//line1
[[64,32]+4.5]//line2
[[5,5]+0.0]//line3
[[5,5]+10]//line3
Respected sir,
My question is :The application invoke main by instant
Sub object b.This invoke Sub costructor second
which immediatelly invoke the Sup constructor second
The Sup constructor output the value received from the
Sub constructor by implicitly calling method toString
and return programe conrol back to the Sub constructor.Then
the Sub constructor output the value by calling toString,same
for Sub c.My question is how the radius become 0.0 in line 1,3.
how it print line 1,3;hope to hear from you.
 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Your Code:

The problem is with toString() function, in sense, you are trying to create the instance of the sub class, so, after the super class constructor is invoked, when u try printing "this", the sub class version of "toString()" is invoked. Since, the sub class data member "radius" is not initialized the 0.0 is printed first, later the sub class construtor initializes the "radius" and then calls the sub class version of "toString()" and hence the value is printed propertly.
Hope this helps u,
Uma.
 
reply
    Bookmark Topic Watch Topic
  • New Topic