• 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

super and this

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
We know that either the "super" or "this" statement should be the first statement in a constructor. What should be done in case I want to invoke both of these from the same constructor?
For eg., consider the following code
<CODE>
class superclass
{
superclass()
{
System.out.println("In superclass() constructor");
}
superclass(int i)
{
System.out.println("In superclass(int) constructor");
}
superclass(String s)
{
System.out.println("In superclass(String) constructor");
}
}
class subclass extends superclass
{
subclass()
{
System.out.println("In subclass() constructor");
}
subclass(int i)
{
super(i);//problem here
this(""+i);//and problem here too!
System.out.println("In subclass(int) constructor");
}
subclass(String s)
{
super(s);
System.out.println("In subclass(String) constructor");
}
}
class superthis
{
public static void main(String args[])
{
subclass obj=new subclass(30);
}
}
</CODE>
When I create an object of the subclass with an int parameter, there occur problems in the lines mentioned in the code. Both "super" and "this" compete for being the first line of the constructor and hence a compiler error results. Is there any way I can take care of this case?
Thanks,
Aparna

 
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
not as far as i know. you can only have one or the other. if your superclass call was to the no args constructor, you could just leave out the call. java would take care of it. I think.
[This message has been edited by Randall Twede (edited December 12, 2000).]
 
Aparna Narayanan
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Randall,
Yeah, I know that super(), super(some args), this(), this(some args) are all mutually exclusive. But my requirement is not uncommon. What should I do when I need to call both from the same constructor? Consider the code I have posted in my question. Such a requirement can commonly arise. How can I take care of such a situation?
Thanks,
Aparna
 
Randall Twede
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
maybe(this is conjecture) you could call like this:
MyProgram() {
this(i);
}
MyProgram(int i) {
super(i);
}
get my drift?
 
Enthuware Software Support
Posts: 4818
52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
>But my requirement is not uncommon
True. And the solution is also quite common.
Move the code from one contructor to a method. Call that method from where ever you want.
HTH,
Paul.

------------------
Get Certified, Guaranteed!
(Now Revised for the new Pattern)
www.enthuware.com/jqplus
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic