• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Inserting call to the constructor of the superclass

 
Tiberius Marius
Ranch Hand
Posts: 115
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In OCA Java SE 7 Programmer I Certification Guide by Mala Gupta at page 322 (Chapter 6 / Working with inheritance ) there is a phrase that i don't think it's entirely correct :

"EXAM TIP If present, a call to a superclass’s constructor must be the first
statement in a derived class’s constructor. Otherwise, a call to super(); (the
no-arg constructor) is inserted automatically by the compiler."

I think this statement omits to mention an important point as well , that if you have a this() or this(parameters) statement the hidden call super() automatically inserted by the compiler does not happen. Another way of saying this is that a constructor will always start with one of the three things :
1.an user defined this() / this(parameters) call to a overloaded constructor in the same class
2.an user specified super() / super(parameters) call to the base class constructor
3.a hidden super() call inserted by the compiler in the code if points 1 and 2 do not happen
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are correct!

The exam tip should be something like: "If present, a call to another constructor of the derived class (using this) or a superclass’s constructor (using super) must be the first statement in the derived class's constructor. If neither of them is present a call to super(); (the no-arg constructor) is inserted automatically by the compiler."

(I notified the author about this thread and your proposal to improve the book)
 
Tiberius Marius
Ranch Hand
Posts: 115
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think i found another issue in Chapter 6 page 339 :


"

Q6-9. Given the following code, which of the options, when applied individually, will
make it compile successfully?

Line1> interface Employee {}

Line2>interface Printable extends Employee {
Line3> String print();
Line4> }

Line5> class Programmer {
Line6> String print() { return("Programmer - Mala Gupta"); }
Line7> }

Line8>class Author extends Programmer implements Printable, Employee {
Line9> String print() { return("Author - Mala Gupta"); }
Line10> }



a Modify code on line 2 to: interface Printable{
b Modify code on line 3 to: public String print();
c Define the accessibility of the print methods to public on lines 6 and 9.
d Modify code on line 8 so that it implements only the interface Printable .

"


The correct response it supposed to be c : Define the accessibility of the print methods to public on lines 6 and 9.

But wait , why do you need the method on line 6 to be public ? I don't see it as necessary for it to compile successfully . I think the correct answer should be only "Define the accessibility of the print methods to public on lines 9."

Why ?
- the class programmer does not implement any interface and it's a base class here (aka no need to force the public modifier from the interfaces )
- a overridden method can expand accessibility of the method down the road in sub-classes (the problem is to not restrict them) and in this case the method gets it's accessibility expanded not restricted , which is fine

 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35279
384
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are correct that only line 9 is required to be public to make the code compile. However, it isn't wrong to make both lines 6 and 9 public. The code still compiles making it a correct answer. The question doesn't ask what the minimal change is to make the code compile.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tiberius Marius wrote:The correct response it supposed to be c : Define the accessibility of the print methods to public on lines 6 and 9.

But wait , why do you need the method on line 6 to be public ? I don't see it as necessary for it to compile successfully . I think the correct answer should be only "Define the accessibility of the print methods to public on lines 9."

You are correct! Marking the print-method on line 9 as public is the only required change to successfully compile the code. And your explanation is also spot-on, so you know your stuff very well

But the question is "Given the following code, which of the options, when applied individually, will make it compile successfully?", not to make this code compile with the least number of changes. If that would have been the question, this would be an issue. But now it's definitely not (in my opinion).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic