Forums Register Login

regarding this or super call in constructors

+Pie Number of slices to send: Send
a call to this or super should be the first line of any constructor if we are providing them?Why cant we have them anywhere in our constructor code?
+Pie Number of slices to send: Send
if you instantiate subclass constructor will run after super class constructor. so super is used to call super class constructor so it should first line.. more over this is used to overload the constructor so it should first line of constructor.

you can use super anywhere inside a method

hope this helps
+Pie Number of slices to send: Send
Not quite "this" or "super", but actually "this()" or "super()".
+Pie Number of slices to send: Send
Since all super types of your class might have internal fields, they have to have one of their constructors run. These super constructor runs have to be completed before your first assignment within your constructor. This makes perfectly sense. Suppose you could place the super() call anywhere within your constructor and a super type had protected member variables which are to be initialized in the super() call. If you would assign values in your constructor and afterwards call super(), your values vould be overridden. Fortunately, these kinds of errors cannot happen. The first call from a constructor is always a call to a this or a super constructor. If the super type has an accessible no-arg constructor this call can be performed implicitly for you. In this case, you don't have to call super explicitly.
+Pie Number of slices to send: Send
Onother example of what would go wrong if calling super() was to be permitted everywhere: It is possible to declare a field final and assign a value in the constructor. In fact, the value then has to be assigned within the constructor. Suppose such a field would be acceesible for an extending class which was allowed to put its super() call anywhere in its constructor...
+Pie Number of slices to send: Send
 

Originally posted by abin joy:
a call to this or super should be the first line of any constructor if we are providing them?Why cant we have them anywhere in our constructor code?


yes, this() and super() both must be called from the first line of the subclass constructor. Object class is a super class of all the classes. So, if one is not defined constructor in the superclass compiler will automatically put super() call in the constructor. Because constructor chaining follows the order of topdown approach. So, one has must call super class constructor from the sub class constructor.
The moth suit and wings road is much more exciting than taxes. Or this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1423 times.
Similar Threads
Constructor not found error
Question on Construtor
Constructors are not inherited.
Rules for Constructors
call super class constructor
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 07:11:39.