• 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

Exam question - constructors- help!

 
Ranch Hand
Posts: 232
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
could someone please explain the following
Which statements concerning the following code are true?
class A {
public A() {}
public A(int i) { this(); }
}
class B extends A {
public boolean B(String msg) { return false; }
}
class C extends B {
private C() { super(); }
public C(String msg) { this(); }
public C(int i) {}
}
a. the code will fail to compile
b. the constructor in A that takes an int as an argument will never be called as a result of constructing an object of class B or C
c. Class C has three constructors
d.objects of class B cannot be constructed
e.at most one of the constructors of each class is called as a result of constructing an object of class C
 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sarim,
i changed the code a little bit...


a. the code will fail to compile
b. the constructor in A that takes an int as an argument will never be called as a result of constructing an object of class B or C
c. Class C has three constructors
d.objects of class B cannot be constructed
e.at most one of the constructors of each class is called as a result of constructing an object of class C

Here are my suggestions
a) FALSE. Compiles without problems
b) TRUE. If you create an Object C with the Constructor in line 11, super() will be implicitly called, BUT this is default constructor of Class B, this one will call default Constructor of Class A. So A(int) will never be called.
NOTE: line 6 is NOT a constructor, it's a method, so the default constructor will be created from the compiler
c) TRUE. line 9, 10, 11 are constructor, so we have three constructors
d) FALSE. You can construct objects of Class B. Default constructor will be created from the compiler
e) FALSE. If you create an object C with C(String msg), the private constructor of C will be called. In this case, two constructors of C were used, so the answer has to be FALSE (Perhaps my english is not so good and i didn't get the meaning of the last answer, but that's my understanding)
Hope that helps,
as always, correct me if i'm wrong
cheers
Oliver
[This message has been edited by Oliver Grass (edited November 10, 2000).]
 
sarim raza
Ranch Hand
Posts: 232
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks oliver
 
reply
    Bookmark Topic Watch Topic
  • New Topic