• 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
  • Devaka Cooray
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Jeanne Boyarsky
  • Tim Cooke
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
Bartenders:

Calling super constructor from subclass

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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,
Sudhir

[This message has been edited by sudhir dharmadhikari (edited March 02, 2001).]
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
1.super((m*100),n);
2.class MyClass2 extends MyClass1{
static public int lm;
MyClass2(int m ,int n) {
super((lm=m*100),n);
}
hope this helps ... Rille
 
Sheriff
Posts: 17628
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
HTH,
Junilu
[This message has been edited by JUNILU LACAR (edited March 03, 2001).]
 
sanju dharma
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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,
Sudhir
 
permaculture is a more symbiotic relationship with nature so I can be even lazier. Read tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic