JAVA inheritance constructor

ps: easy to read because ... I like to call a method called function

1) a single class, if not declare any constructors, the system will automatically generate a no-argument constructor, this time, new classA () without error. ! If you explicitly declare a constructor parameter, then use the new classA (no arguments) will report an error, because the system in the existing situation does not help generate a constructor with no arguments, it is recommended to add their own a no-argument constructor

--- a class constructor will only have three conditions: with no arguments / parameters without parameters + have / have parameters

2) If the inheritance relationship exists, it is assumed classB extends classA, all constructors subclasses (including the no-argument constructor, there is argument constructor), if not a kind of super-class constructor explicitly call will default to call super (), That is: no parent class constructor parameter, if this time is not the parent class constructor without parameters, it is recommended ~ given parent add a constructor with no arguments

(Example:

A has only one argument constructor, B has a constructor with no arguments, this time being given

A is added to a non-argument constructor, not given

3) in the hierarchy, when using a new class, program execution sequence: first implementation of the parent class's constructor method, then perform its own constructor method, the order of execution can not be changed, otherwise an error or not run.

4) the depth of the hierarchy, i.e. when there is inheritance C B, B inherits A, inherited or more layers, the uppermost layer is performed first constructor, in turn passed on along the chain of succession, until    the object class is created . For example: we declare C object, it called the C constructor c, constructor c calls the parent class constructor of B b, constructor b calls the parent class A constructor in A , then the execution result is : a => b => c 

 

-problems:

The constructor subclass can override the parent class constructor cover of?

8 can of course Oh. In java, a subclass can not inherit the parent class constructor, so you can not override the parent class constructor. Only the parent class constructor is used by an explicit call super (null / parameters), or is no argument constructor call the parent class already defined,

 

Imagine: If a subclass overrides the parent class constructor override: you need both methods were the same, but the name of the method is a constructor class name, so that: - the absence of such

Remember: you must call the parent class constructor initializes, super () statement must be a subclass constructor head a statement!

Guess you like

Origin www.cnblogs.com/Xavier-h/p/11671125.html