my dog learned polymorphism
The moose likes Java in General and the fly likes Calling super constructor from subclass Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Calling super constructor from subclass" Watch "Calling super constructor from subclass" New topic

Calling super constructor from subclass

sanju dharma
Ranch Hand

Joined: Oct 19, 2000
Posts: 45
Hi ,
Since "super()" should always be first line in constructor of subclass, how can I pass an argument which member of subclass to super ? Following is simplified code which does NOT work.
public class MyClass1 {
MyClass1() {
System.out.println("Default constructor in MyClass1");
MyClass1(int j,int k) {
System.out.println("Two int arguments in MyClass1");
class MyClass2 extends MyClass1{
public int lm;
MyClass2(int m ,int n) {
lm = m * 100; // I should construct lm here
super(lm,n); //then i need to pass lm to super.
Please note that , this is simplified code. Actually , lm is an object which i have to construct in MyClass2 constructor with the help of value "m". Anybody to suggest correction ?
Thanks in advance,

[This message has been edited by sudhir dharmadhikari (edited March 02, 2001).]

Joined: Feb 06, 2001
Posts: 6
Hi please forgive me for my bad english...
Well because lm dont exist umtil a objekt is created, make it static so it will be "created" when the class is loaded,then you can pass the product of m*100 to the super constructor
or you can leave the lm out and only pass down the product
2.class MyClass2 extends MyClass1{
static public int lm;
MyClass2(int m ,int n) {
hope this helps ... Rille

Sun Certified Programmer for Java 2 Platform
Junilu Lacar

Joined: Feb 26, 2001
Posts: 6529

Seems like you need a little refactoring here, pardner.
In your code, Im is defined in MyClass2, yet is somehow used to initialize the superclass MyClass1. It would seem to me then that Im needs to be declared as a member of the superclass MyClass1 instead. That way, your MyClass2 constructor would just call super() or super(valueUsedToInitializeImInSuperclass). Would seem a lot cleaner than trying to write "clever" code to workaround the "super() as first statement" requirement of constructors.
Also, I would look closely at the relationship between Im and MyClass1 and MyClass2. Look into whether it would be more logical to define Im (which you say is actually an Object) as a protected or public inner class of MyClass1.
[This message has been edited by JUNILU LACAR (edited March 03, 2001).]

Junilu - [How to Ask Questions] [How to Answer Questions]
sanju dharma
Ranch Hand

Joined: Oct 19, 2000
Posts: 45
Thank you very much for your suggestions Rille & Junilu.. unfortunately MyClass1 is Sun's Java library class , so I cant(i dont want to) make lm object as super class member.
Making lm object static is a also a good idea. But(!), i have significant(around 10 lines) code to operate on lm object before passing it to super class. I can try to create lm object in my main program & pass it to MyClass2 constructor. That is simplest. But I am reluctant for this compromise. Also , making lm a class variable my bother my multi-threaded program.
Comments please .
Thanks & regards,
I agree. Here's the link:
subject: Calling super constructor from subclass
It's not a secret anymore!