aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Self Test Question of K&B. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Self Test Question of K&B." Watch "Self Test Question of K&B." New topic
Author

Self Test Question of K&B.

Syed Naimathullah
Greenhorn

Joined: Feb 18, 2007
Posts: 17
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

Joined: Apr 23, 2006
Posts: 44
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

Joined: Apr 22, 2006
Posts: 424
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

Joined: Jun 24, 2007
Posts: 40
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 ]
 
 
subject: Self Test Question of K&B.