Is it mandatory to call the constructor of the base class from the constructor of a class inherited from it?

0

My problem is the following, when inheriting from a class (Base, whether abstract or not) which has one or more constructors defined explicitly I am forced to call some of them in any constructor that defines the derived class because if I do not, he sends me an error message.

On the other hand, if I try to call the default constructor from the derived class and it is not explicitly defined by me, it also sends me an error.

I want to know why it does not call constructor by default when it is implicit in the code and also if it is mandatory to call the constructors from the derived classes.

    
asked by Luis Ospino 24.02.2018 в 02:16
source

2 answers

-1

When you create an object in Java, the following operations are performed automatically:

  • Memory is allocated for the object.
  • The attributes of that object are initialized to the default values by the system.
  • The constructor of the class is called.
  • Note: if no constructor method is defined for a class, one is automatically created by default .

    The default constructor is a constructor without parameters that does nothing. The attributes of the object are started with the default values by the system.

    Example:

    public Class Persona {
    
        private String nombre;
        private String apellido;
    
        // Cero constructor
    
    }
    
    public Class MainPersona {
        public static void main(String[] args) {
    
            Persona persona = new Persona();
    
            // Se llama al constructor por defecto
    
        }
    }
    

    A default constructor (empty) looks like this:

    // Constructor vacío
    public Persona() {
    }
    

    When you instantiate a class, the parent class must have at least one constructor, even if empty, but you must have one. If you do not have it, you will receive the error:

      

    The constructor Person () is undefined

    Then, to solve this error you must have the empty constructor in the following way:

    public Class Persona {
    
        private String nombre;
        private String apellido;
    
        /*
         * Constructor vacío
         * Evita el error "The constructor Persona() is undefined"
         * cuando se instancia esta clase en alguna otra clase
         */
         public Persona() {
             super();
         }
    
    }
    

    If you need another constructor in the parent class, you can have it, you can have the number of constructors you need, example:

    public Class Persona {
    
        private String nombre;
        private String apellido;
    
        /*
         * Constructor vacío
         * Evita el error "The constructor Persona() is undefined"
         * cuando se instancia esta clase en alguna otra clase
         */
         public Persona() {
             super();
         }
    
         // Constructor
         public Persona(String nombre, String apellido) {
             super(); // Llama al constructor vacío primero
             this.nombre = nombre;
             this.apellido = apellido;
         }
    
    }
    

    I hope I have helped you!

        
    answered by 24.02.2018 / 04:51
    source
    4

    When there is inheritance in Java, and the parent class does not have a default constructor, it is mandatory to explicitly call the constructor of the parent class from the constructor of the daughter class.

    I'll explain a little about the concepts:

    All classes in Java must have at least one constructor, even if you do not declare it explicitly. That constructor is called the constructor by default and is equivalent to declaring a constructor that does not receive parameters. Now, if it is not explicitly declared, the compiler adds it for you at compile time, this guarantees that all classes have at least one Java constructor.

    In addition to the above we have something very similar. If a class has a parent, when the parent is invoked, the constructor of the parent class must first be invoked.

    To call the constructor of the parent class we must use the following statement super() (without parameters in case of calling the constructor by default). This has to be the first line inside the constructor code of the daughter class (except if instead of super() we use this() , where we would be calling another constructor of the same daughter class, but inside this other constructor the first line has to be the call to super() ).

    As with the default constructor, if we omit this line in our code, the compiler adds it at compile time, but will try to invoke the default constructor of the parent class. Therefore, if the parent class does not have a default constructor, we have to explicitly declare in the constructor of the daughter class, the call to the corresponding constructor of the parent class.

    I know it may seem like a mess but I tried to explain it as simply as possible.

        
    answered by 24.02.2018 в 03:26