• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

super has to be the first statement in subclass constructor

 
amal shah
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure if there is a way to check it before, but how about this workaround?

 
Ronald Schild
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).





 
Henry Wong
author
Marshal
Pie
Posts: 21437
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
amal shah
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks henry...that helped
 
Rob Spoor
Sheriff
Pie
Posts: 20611
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob is a harsh boss.

Another possibility is to make the constructor private, and use a factory method:
 
fred rosenberger
lowercase baba
Bartender
Posts: 12186
34
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49849
70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21437
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20611
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic