File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes super has to be the first statement in subclass constructor Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "super has to be the first statement in subclass constructor" Watch "super has to be the first statement in subclass constructor" New topic
Author

super has to be the first statement in subclass constructor

amal shah
Ranch Hand

Joined: May 05, 2006
Posts: 92
Hello,
I have 2 classes as follows :





I know super(x,y) statement has to be the first statement in the subclass's constructor.

But before calling super(x,y) i want to check for valid values of x and y, if invalid i want to throw IllegalArgumentException.

Is there any way for checking x and y values before calling the superclass's code.

help appreciated
amal

Dennis Korbar
Greenhorn

Joined: Jan 14, 2009
Posts: 20
I'm not sure if there is a way to check it before, but how about this workaround?

Ronald Schild
Ranch Hand

Joined: Jun 09, 2008
Posts: 117
If it is a check on x and y that you want to do before calling super, odds are this check is something you want to place
in the constructor of the super class.

If it is a check that arranges class specific details that do not go for the super class, odds are this is something you want to
place in the subclass. This will go after super(), because it is class specific (there's no reason to try and handle subclass specific
stuff at the moment you're in the process of handling super class stuff during object creation - you build the foundation of a
house before you put up the walls that carry the roof).






Java hobbyist.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18715
    
  40


Agreed. In this case, it is best to check the args after the call to super(). If those args are bad for the super, then the super should throw the exception.

However, if you still really want to do this, here is another work-around...



And BTW, it isn't necessary to declare "throws IllegalArgumentException" since the IAE is a runtime exception.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
amal shah
Ranch Hand

Joined: May 05, 2006
Posts: 92
thanks henry...that helped
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19670
    
  18

Dennis Korbar wrote:I'm not sure if there is a way to check it before, but how about this workaround?


If I were your employer and you would hand in that code, I would fire you on the spot. Seriously. Well ok, maybe not fire you, but definitely yell at you.

Classes should never need to know anything about which sub classes it has, and your workaround clearly violates that.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3007
    
    9
Rob is a harsh boss.

Another possibility is to make the constructor private, and use a factory method:
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11229
    
  16

he may be a harsh boss, but he is correct. what would happen if I then created a class C extends A.

I would be hosed for no good reason.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38353
    
  23
Rob Prime wrote:If I were your employer and you would hand in that code, I would fire you on the spot. Seriously. Well ok, maybe not fire you, but definitely yell at you.

Classes should never need to know anything about which sub classes it has, and your workaround clearly violates that.
Mike Simmons wrote:Rob is a harsh boss.
Rob is a kind gentle man. It is impossible for a class to know about its subclasses, and handing in that sort of code merits a slow and painful death.
Dennis Korbar
Greenhorn

Joined: Jan 14, 2009
Posts: 20
eeek, it was the only quick way I could think up for doing what was requested... sorry about that, just wanted to help, didn't say that it was the appropriate way to do things...
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18715
    
  40

This is why I tend to not say much duing a code review. One wrong off-the-cuff statement, and bam, you get disemboweled ...

Henry
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19670
    
  18

Dennis Korbar wrote:eeek, it was the only quick way I could think up for doing what was requested... sorry about that, just wanted to help, didn't say that it was the appropriate way to do things...

And we do appreciate your input very much. It's just that once somebody learns that trick, they may actually start using it. And although it works, it is really bad practice.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: super has to be the first statement in subclass constructor