Analysis of problems with subclasses calling superclass constructors in Java

  • 2020-04-01 01:43:20
  • OfStack

In Java, the constructor of the parent class must be called during the construction of the subclass, because when there is an inheritance relationship, the subclass must inherit the contents of the parent class. By what means?      

Here's the answer:      

  When you are new to a subclass object, must first to the new one parent to like, the superclass object is located inside a subclass object, so the subclass object is greater than the superclass object, and a subclass object which contains a parent class, this is the true story in memory. A constructor is new object, must adjust method, this is the rules, to new superclass object, so sure to call the constructor, so:      

  First rule: during the construction of a subclass, the constructor of its parent class must be called. A class, if we don't write a constructor, the compiler will help us to add a default constructor, the default constructor, which has no parameter constructor, but if you write a constructor, the compiler will not give you added, so sometimes when you are new to a subclass object, must call the subclass constructor, but we did not show in the subclass constructor to call the base class constructor, just can't write, such as: super (); It's not written this way, but it calls the parent class's constructor without arguments, and if the parent class doesn't have a constructor without arguments, it will fail.      

  The second rule: if a subclass constructor not shown in the invocation of the base class constructor, then the system default parameterless constructor note: calling base if the subclass constructor is neither shows the invocation of the base class constructor, and in the base class has no default no-parameter constructor, the compiler error, so, we usually need to display: super (parameter list), to call the superclass constructor parameters.


//When you do not use the parent class's default constructor, the constructor defined by the parent class needs to be displayed in the subclass's constructor.
 class Animal{
   private String name;

   //If you define a new constructor
   public Animal(String name) {
     this.name = name;
   }
 }

 public Dog extends Animal{

   //At this point you need to show the constructor that calls the superclass, because the subclass calls the superclass by default
   //Parameter free construction method Animal()
   public Dog(){
     super(" The dog ");  //Shows the argument constructor that invokes the parent class

     ....  //Constructor handling of subclasses
   }
 }

 //Of course, if you write out the argument free constructor in the parent class, for example:
 class Animal{
   private String name;

   //A parameter-free construction
   public Animal() {
     .....  //To deal with
   }

   
   public Animal(String name) {
     this.name = name;
   }
 }

Summary: in general, however, overloading the constructor is used in the parent class, and the corresponding parent constructor can be called in the subclass as needed.


Related articles: