• 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

Programmer's Final Exam question #13 (from imon Robert's book)

 
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Question #13

Q: What is the effect of attempting to compile the following code and execute the Child application?
A. Compiler error at line 6
B. Compiler error at line 7
C. Compiler error at line 9.
...
-----------------------------------------------
The book's answer is B line #7 where the Child() constructor is written.
I disagree and say it happens because of line 6 - my reason is that I would save the above code as Child.java because I expect to execute as java Child (where the static main method is contained).
If I proceed on that assumption, I get the following report from my sun jdk1.4 compiler:

C:\BIN>javac Child.java
Child.java:1: class Papa is public, should be declared in a file named Papa.j
a
public class Papa {
^
Child3.java:9: cannot resolve symbol
symbol : constructor Papa ()
location: class Papa
Child3() {
^
2 errors


So I am saying that we need to declare Child as the public class and remove the public modifier from Papa.
If we do that, we still have to account for the next compiler error which tells us that there will be an error at line 7 because the no-arg call to the Child() constructor will attempt to call the no-arg constructor in Papa - which is not there because we defined a 1 arg constructor therefore we don't get the no-arg up there by default.
[b] Can anyone give me some feedback - I hope the exam will not contain such a misleading set up which leads to such a poor choice of correct response.
 
david eberhardt
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
stupid me
correction to my own assumptions above:
The book is right!
I saved the file as Papa.java and compiled with javac Papa.java - results in compiler error pointing at line 7:

this is because there is NOT a no-arg constructor in Papa that is being looked for from the Child() no-arg constructor. Since we defined a 1 arg constructor in Papa, a default no-arg constructor is not provided in that class.

When I add a no arg constructor in Papa, I can compile Papa.java and get two class files (Papa.class and Child.class).
I can run "java Child" from the command line - this is the class that contains the static main() method.
- I guess I had become used to saving the file with the static main method() as the name.java source file.
 
Ranch Hand
Posts: 279
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, it's not a poor setup. First, if you declare a class public or not, you can still runn it (java ClassName) BUT the source file name must have the same name as the public class - if one exists in the source file - so this is not the problem. The 2 errors that you got compiling the file is because you named the source file in a wrong way, it should be Papa.java
The question is actually examining your knowledge about constructors, super calls in inheritance and default constructors.
1- A class with no exciplicit constructors offers an implicit default constructor with no arguments. If you declare ANY constructors in the class, no implicit default constructors are offered.
2- A subclass constructor with NO explicit call to super() as the first statement in the body of the constructor will implicitly call the default constructor in the parent class as its frist statement.
in the code, there will be NO implicit constructor in the Papa class, and the constructor of the Child class doesn't explicitly call the provided constructor in the Papa and thus the compiler will implicitly call the default constructor in Papa - which doesn't exist - and thus cause a compile error.
HTH
 
david eberhardt
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Alfred,
Thanks for your input. As you can see from my second post, I figured out the errors of my assumptions!
anyways, here's an important point to reinforce:
the superclass no-arg constructor always get's called by the subclass constructor IF no explicit call to any other constructor in the parent is made (i.e. super(j) in the example below)
 
david eberhardt
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
to reinforce Alfred's comments;
if you define any constructors at all in the Papa (parent) class , we better have a no arg constructor explicitly defined up there also!
otherwise, you get the compiler error.
Thanks.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic