Explanation how super () method works in Java

I have a problem with the super method but I don't really know what to do to make it work. Help!

public class ClassUno {

    public static void main(String[] args) {
    }

    ///// Clases Internas   ////

    class Primera {

        int k;
        public Primera( int s ) {
            k = s;
        }
    }

    class Segunda extends Primera {

        public Segunda() {
            super();
        }

        public Segunda( int n ) {
            k = n;
        }
    }
}
 0
Author: HCarrasko, 2016-04-17

2 answers

When using inheritance in Java, in Constructors you declare in child classes they should always call super(args) or this(args), where:

  • super(args) refers to a constructor of the parent class.
  • this(args) refers to a constructor defined in the current class.
  • args in super(args) and this(args) refers to the possible arguments required by the constructor being called.

Note that in Constructors of a class child, at least one constructor must call super() and the other constructors can call super() or this().

This is explained in the Java specification, Chapter 8 classes, part 8.8 Constructor declarations, section 8.8.7 Constructor body .

Knowing this, let's look at the definition of Primera:

class Primera {
    int k;

    public Primera(int s) {
        k = s;
    }
}

It can be observed that there is only 1 constructor that receives 1 argument of type int. Now let's look at the constructors of Segunda:

class Segunda extends Primera {
    public Segunda() {
        /*
            error de compilación
            Causa: No existe un constructor en la clase padre
            sin argumentos
        */
        super();
    }


    public Segunda( int n ) {
        /*
            error de compilación
            Causa: no se llama a super(args) o this(args) en la primera línea
        */
        k = n;
    }
}

This can have multiple solutions depending on the design of your classes. Here is an example:

class Segunda extends Primera {
    public Segunda() {
        /*
            se llama al constructor Segunda(int)
            y se envía como argumento 0
        */
        this(0);
    }


    public Segunda( int n ) {
        /*
            se llama al constructor de la clase padre
            con argumento n. En la clase padre, va a inicializar
            el valor del campo k con el valor de la variable n
        */
        super(n);
    }
}
 8
Author: , 2016-04-18 03:46:40

I would think that your problem is that when you put the super you have to pass as a parameter the attributes it has in the first class. In your case it is the variable "s". Then you would look like this:

public static void main(String[] args) {
}

Internal Classes

class Primera {

    int k;


    public Primera( int s ) {

        k = s;
    }
}

class Segunda extends Primera {

// aca es donde le pasas por parametro la variable s. si tuvieras mas variables deberias de pasarle todas las variables como parametro 

    public Segunda() {


        super(s);
    }


    public Segunda( int n ) {

        k = n;
    }
}
 0
Author: Nicolas Schmidt, 2016-04-18 10:53:45