Java: Abstract class constructors and this() -


Can someone tell me what's wrong?

I have two classes, one essence and one solid, as follows:

  Public abstract class abstract {protected stat. ORDER = 1; Public stationary zero main (string [] args) {concrete c = new concrete ("hello"); } Public abstract () {class C = this.getClass (); System.out.println (ORDER +++ ": class =" + c.getSimpleName (+) + "; is called the no-argor controller of the essence."); } Public essence (string AGR) {it ()); Class C = this.getClass (); System.out.println (ORDER +++ ": class =" + c.getSimpleName (+) "is called the 1-arrow constructor of the essence."); }}  

and

  public square concrete summary {public concrete () {super (); Class C = this.getClass (); System.out.println (ORDER +++ ": class =" + c.getSimpleName () + "; is called a concrete no-argor controller."); } Public Concrete (String AGR) {Super (AGR); Class C = this.getClass (); System.out.println (ORDER ++ + ": class =" + c.getSimpleName (+) + "; is called 1-RG constructor of concrete."); }}  

When I run it, I get the following output:

1) Class = concrete; Abstruct is called a non-constructor constructor.
2) Class = Concrete; The abstract is called 1-RG constructor.
3) Class = concrete; 1-RG Constructor of Concrete is called.

My question is: Why is not the call from the abstract string ab constructor, it calls on non-origin constructor at concrete? Or, perhaps more consistently, is there any way to get the abstract string ARG constructor to call the non-constructor constructor on the concrete in some way, which allows the "proper" chaining of the constructors?

no - constructor chaining always either sideways (in the same type) ) Or above (for parents type).

Do not forget that the call is to be resolved on compile-time - and abstract does not know what else classes should be achieved from it Can you call a virtual method within the constructor

you can abstract constructor, and that method will be < Code> concrete . ... but I urge not to do so, in particular, the constructor body for concrete will not yet be executed, nor will the variable initializers - it will not be able to do anything with concrete - specific state some lot are specific situations where this is the right thing to do, but they are rare and They should be handled with care.

What are you actually trying to do? Normally I think it is better to move several "sideways" chains to a constructor in which there is a "upward" series.


Comments

Popular posts from this blog

c# - How to capture HTTP packet with SharpPcap -

jquery - SimpleModal Confirm fails to submit form -

php - Multiple Select with Explode: only returns the word "Array" -