• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

kathy sierra scjp 1.6 book question doubt

 
dilip kumarr
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

question:
what is the result?

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(" ");
}
}

doubt:
1.wats the meaning of : new Bottom2("c"); in the above program.

kindly clear my doubt...........

thanks in advance..............



 
Minhaj Mehmood
Ranch Hand
Posts: 400
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

it means you are calling the overloaded constructor of Bottom2 class.
 
Shailesh Phatak
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
since the base class does not have an explicit zero arg contructor it will produce an error
 
Minhaj Mehmood
Ranch Hand
Posts: 400
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
by the way there are some errors in your code...
 
Nur Ahmed
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think M M kaimkhani already gave you the answer in his first response.

new Bottom2("c") is the call to the constructor Bottom2 which has an implicit call to the super (not visible in this piece of code though. you have to code it in as if you have a constructor with arguments the compiler will not provide you a similar one or a default one). //using braces in java is tricky ... :-) all I meant is, for the minimum, you will need to add something like super(""); in the Bottom2 constructor.

So, to sum up, the logic/catch is to call the super class constructor first. Try running the code without "new Bottom2("c")" ;-)


 
Afzal Rehman
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all code is wrong
1- There is } closing bracket is missing.
2- There is no no-arg constructor is in Top class.

So Code will fail ...

 
Ankit Garg
Sheriff
Posts: 9529
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dilip please Use Code Tags when you post a source code. That way your code looks formatted. Unformatted code is hard to read. You can add code tags by wrapping your code in [code] [/code] tags. You can edit your message using button and then add code tags to it...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic