• 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

[Exercise] Prove that base class constructor is always called

 
Ranch Hand
Posts: 782
Python Chrome Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Fellow java'ers,
TIJ has a question that reads:
Prove that the base-class constructor are
(a) always called, and
(b) called before derived-class constructors.
My idea for (a) follows. Your suggestions welcomed.
Assume that the base class is A and the derived class is B.
e.g. class B extends A{ ... }
I could make the constructor private in the base class.
That way I know for sure that the instantiation of class B may fail if it can't find a ctor in A.

/// B.java
class A{
private A() { System.out.print("A ctor"); }
}
public class B extends A{}
Compiling this gives me the error:

Is this formal enough ? Have I covered all the bases?
Anyone want to give (b) a try?
Thanks
Pho
 
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Pho,
I don't think your example answers any of the questions. Getting a compiler error rarely proves anything! The following code can be used to prove both a and b.

Regards,
Manfred.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic