The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
July 2001

Advertisement

Advertisement

This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.

Message:

Call non-default ancestor's constructor from descendent constructor

Posted by Kishori Sharan on July 19, 2001 at 9:32 PM

When you inherit a class from other class then by default the java compiler puts a call to the default construtor of ancestor class. However, if in the construtor of descendent class you call constructor of ancestor then java compiler doesn't call the default construtor of ancestor. Therefore, in your case call the non-default construtor of class A from both the constructor of class B and everything will be OK. Here is your example.
///////// A.java
public class A{
String a ;
A(String a) {
this.a = a;
}

}

class B extends A {
B ( ) {
super ( "hello" ) ;
}
B ( int i ) {
super ( "hello" ) ;
}
}



Replies:

Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us