Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Self Test Question of K&B.

 
Syed Naimathullah
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can anybody explain me the working of this program........?

class Top{
public Top(String s)
{
System.out.print("B");
}
}

public class Bottom2 extends Top
{
public Bottom2(String s)
{
System.out.print("D");
}

public static void main(String[] args)
{
new Bottom2("C");
System.out.println(" ");
}
}


The output is :"compilation fails".
Iam not able to understand how does the above output comes( ).
 
Amrit Kashyap
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you need to provide default no argument constructor for your Top class, since if you overload constructor then jvm doesn't provide the default one.
 
ahmed yehia
Ranch Hand
Posts: 424
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Bottom2 Constructor the compiler inserts a no-arg call to super(), which it didnt find because your class Top doesnt define any no-arg Constructor so compilation fails
 
AtulKumar Gaur
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just modified the code to make you understand ,Now it will work fine because we have provided the no-args constructor for Top class.
My ranchers friends are right when you overload the constructor my good friend JVM does not provide the default constructor(non args constructor).So in that case we have to provide it explictly.
class Top{
public Top(){

}
public Top(String s)
{
System.out.print("B");
}
}

public class Bottom2 extends Top
{

public Bottom2(String s)
{

System.out.print("D");
}

public static void main(String[] args)
{

new Bottom2("C");
System.out.println(" ");
}
}
[ June 27, 2007: Message edited by: Jim Yingst ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic