File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes [Exercise] Prove that base class constructor is always called Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "[Exercise] Prove that base class constructor is always called" Watch "[Exercise] Prove that base class constructor is always called" New topic
Author

[Exercise] Prove that base class constructor is always called

Pho Tek
Ranch Hand

Joined: Nov 05, 2000
Posts: 761

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


Regards,

Pho
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: [Exercise] Prove that base class constructor is always called