Results 1 to 5 of 5
Thread: this() and super() ?
- 08-13-2009, 08:57 AM #1
Member
- Join Date
- Aug 2009
- Posts
- 1
- Rep Power
- 0
- 08-13-2009, 11:55 AM #2
In constructor either this() or super() should be the first one to get executed.
U can't give both.
My question is why u got the necessity to use both in ur constructor?
Always by default constructor refers the current scope (i.e) this().why u need to call explicitly?Ramya:cool:
- 08-13-2009, 01:25 PM #3
Member
- Join Date
- Aug 2009
- Posts
- 21
- Rep Power
- 0
if u implemeted some abstract class u can use super() method
otherwise use "this "
it referes the current class:) javadeveloper
-
I'm sorry but this reply is completely wrong. Use of super has nothing to do with abstract classes. It all has to do with whether you want your constructor to first call a specific constructor of the super class or if you wish it to call another constructor of the current class.
- 08-13-2009, 04:56 PM #5
Senior Member
- Join Date
- Jun 2008
- Posts
- 339
- Rep Power
- 5
I suppose it's possible that if the superclass has a parameterized constructor that isn't called from the default subclass constructor, but the default subclass constructor does some required initialization, a parameterized subclass constructor might want to do both, so it's tempting to think that explicit calls to both are required from the parameterized subclass constructor:
An easy solution is to abstract the default subclass initialization into a subclass method that the parameterized subclass constructor can also call.Java Code:class Super { private int x; public Super() { this(10); } public Super(int x) { this.x = x; } } class Sub extends Super { private int y; // default constructor does local init and wants default superclass init public Sub() { y = 5; ... // etc. } // constructor wants default local init and custom superclass init public Sub(int x) { // this() ?? we'd like this too... super(x); } }
Similar Threads
-
super
By dj kourampies in forum New To JavaReplies: 5Last Post: 01-23-2009, 03:07 PM -
this() vs super()
By Hevonen in forum New To JavaReplies: 9Last Post: 12-04-2008, 01:43 PM -
Super CSV 1.20
By JavaBean in forum Java SoftwareReplies: 0Last Post: 11-27-2007, 08:22 PM -
Super CSV 1.15
By JavaBean in forum Java SoftwareReplies: 0Last Post: 10-16-2007, 06:37 PM -
Use super. or this.
By Marcus in forum New To JavaReplies: 1Last Post: 07-05-2007, 06:52 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks