wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes super and this Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "super and this" Watch "super and this" New topic
Author

super and this

Aparna Narayanan
Ranch Hand

Joined: Nov 07, 2000
Posts: 44
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

Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4340
    
    2

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).]


SCJP
Visit my download page
Aparna Narayanan
Ranch Hand

Joined: Nov 07, 2000
Posts: 44
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

Joined: Oct 21, 2000
Posts: 4340
    
    2

maybe(this is conjecture) you could call like this:
MyProgram() {
this(i);
}
MyProgram(int i) {
super(i);
}
get my drift?
Paul Anilprem
Enthuware Software Support
Ranch Hand

Joined: Sep 23, 2000
Posts: 3253
    
    2
>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


Enthuware - Best Mock Exams and Questions for Oracle/Sun Java Certifications
Quality Guaranteed - Pass or Full Refund!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: super and this
 
Similar Threads
Q. about constructors (and inheritage)
what is super?
constructor question
doubt in constructor
Can you please clarify my doubt ?