• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

compiling this... error

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i came across this question and was not satisfied with the explanation (informit)
========================================================================
1. public class Demo extends Object {
2. String Title;
3. public Demo( String t ){
4. Title = t;
5. }
6. public void showTitle() {
7. System.out.println( "Title is " + Title );
8. }
9. }
10. class DerivedDemo extends Demo {
11. public void setTitle( String tt ) { Title = tt ; }
12. }
What happens if we try to compile this code, create a DerivedDemo object, and immediately call the showTitle method of that object?
A) The message "Title is null" is written to standard output.
B) The compiler complains about the DerivedDemo class.
C) A NullPointerException is thrown in line 7.
========================================================================
my initial answer is "A" but according to the result the answer should have been "B"...
honestly, i dont get it...
please forgive my overly pathetic question... i am a newbie and wanted to take the scjp in a few month... so please help me...
thanks..
ali
 
Ranch Hand
Posts: 205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ali,
With my knowledge, this programme will not compile. In sub class DeriveDemo, you are not having any constructor that calls the super class constructor that you have provided in your programme. Means,
for sub class DerivedDemo, compiler will create a default constuctor and in this it will try to call the super class's default constuctor and there is no default constructor in the super class. Hence the compiler error.
I hope you will understand.
 
ronald ali mangaliag
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks!!!
that's big help...
ali
reply
    Bookmark Topic Watch Topic
  • New Topic