• 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Self Test Question of K&B.

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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( ).
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ew. You guys are ugly with a capital UG. Here, maybe this tiny ad can help:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic